0
An Introduction Object Oriented PHP
Who's this guy, then? <ul><li>PHP Developer since 2000; full-time since 2003
PHP Developer for Zend Technologies since 2005
Zend Framework contributor since January 2006
Zend Framework project lead since April 2009
I live and breathe OOP daily </li></ul>
Who Are You? <ul><li>How many of you have used OOP before?
How many of you use OOP regularly?
If you're not using OOP, why are you interested? </li></ul>
What is OOP?
Object Oriented Programming Models software concepts as discrete  entities , containing  attributes  and capable of perfor...
Start with what you know
Constants and Variables define ( FOO ,   'foo' ); define ( BAR ,   'bar' ); $value   =   'bar' ; switch   ( $value ) { cas...
Associative Arrays $person   =   array ( 'last_name'   =>   &quot;Weier O'Phinney&quot; , 'first_name'   =>   &quot;Matthe...
Functions function   mail ( $to ,   $subject ,   $body ) { // ... do some stuff // optionally return something: return   t...
Objects group related subjects
Start with a class declaration class   Person { }
Define some common class member  variables class   Person { public   $firstName ; public   $lastName ; public   $occupatio...
Some types of class members <ul><li>Variables Declared using a visibility operator and normal variable naming
Constants Declared with the “const” keyword, and no quotes
Methods Declared with the “function” keyword </li></ul>
define methods, which are  functions inside a class class   Person { // ... attributes ... public function   boast () { re...
Use your objects! $matthew   =   new   Person (); $matthew -> firstName   =   'Matthew' ; $matthew -> lastName   =   &quot...
Why bother?
It's good programming <ul><li>Scope and encapsulation
Re-use  (both re-using a class by instantiating many objects, and class extension)
Type enforcement
Testability and maintenance </li></ul>
Some words on scope <ul><li>Methods have access to: </li><ul><li>Whatever is passed to them
Any class member variables, constants, or methods (with respect to visibility) </li></ul><li>Use “ $this-> ” within a clas...
Use “ self:: ” to access member constants </li></ul>
What is this “visibility” stuff? <ul><li>Determines scope in which a class member may be accessed. </li><ul><li>Public : c...
Protected : can be accessed within the class definition, and by extending classes.
Private : may only be accessed and modified within the defining class. </li></ul></ul>
Extension? <ul><li>Use the  extends  keyword: </li></ul>class   Matthew     extends   Person “ Extends” keyword!
Extension: override properties class   Matthew   extends   Person { public   $firstName   =   'Matthew' ; public   $lastNa...
Extension: override methods class   Matthew   extends   Person { public function   boast () { // get parent value from met...
Extension: use the new class $matthew   =   new   Matthew (); echo   $matthew -> boast (); // Matthew Weier O'Phinney is a...
Abstraction <ul><li>A formal way of defining extension points
Two types: </li><ul><li>Interfaces
Abstract Classes </li></ul></ul>
Interfaces <ul><li>Defines a public contract for classes that implement it
Ensures consuming classes will always have specific methods available </li></ul>
Upcoming SlideShare
Loading in...5
×

Object Oriented PHP: An Introduction

4,380

Published on

Overview of OOP basics in PHP.

Published in: Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,380
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
238
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Transcript of "Object Oriented PHP: An Introduction"

  1. 1. An Introduction Object Oriented PHP
  2. 2. Who's this guy, then? <ul><li>PHP Developer since 2000; full-time since 2003
  3. 3. PHP Developer for Zend Technologies since 2005
  4. 4. Zend Framework contributor since January 2006
  5. 5. Zend Framework project lead since April 2009
  6. 6. I live and breathe OOP daily </li></ul>
  7. 7. Who Are You? <ul><li>How many of you have used OOP before?
  8. 8. How many of you use OOP regularly?
  9. 9. If you're not using OOP, why are you interested? </li></ul>
  10. 10. What is OOP?
  11. 11. Object Oriented Programming Models software concepts as discrete entities , containing attributes and capable of performing actions that may optionally use those attributes.
  12. 12. Start with what you know
  13. 13. Constants and Variables define ( FOO , 'foo' ); define ( BAR , 'bar' ); $value = 'bar' ; switch ( $value ) { case FOO : // do something; break ; case BAR : // do something else break ; }
  14. 14. Associative Arrays $person = array ( 'last_name' => &quot;Weier O'Phinney&quot; , 'first_name' => &quot;Matthew&quot; , 'occupation' => 'PHP Developer' , 'gender' => 'Male' , 'email' => 'matthew@example,com' , );
  15. 15. Functions function mail ( $to , $subject , $body ) { // ... do some stuff // optionally return something: return true ; }
  16. 16. Objects group related subjects
  17. 17. Start with a class declaration class Person { }
  18. 18. Define some common class member variables class Person { public $firstName ; public $lastName ; public $occupation ; public $gender ; public $email ; }
  19. 19. Some types of class members <ul><li>Variables Declared using a visibility operator and normal variable naming
  20. 20. Constants Declared with the “const” keyword, and no quotes
  21. 21. Methods Declared with the “function” keyword </li></ul>
  22. 22. define methods, which are functions inside a class class Person { // ... attributes ... public function boast () { return sprintf ( '%s %s is a %s' , $this -> firstName , $this -> lastName , $this -> occupation ); } } Instance variables!
  23. 23. Use your objects! $matthew = new Person (); $matthew -> firstName = 'Matthew' ; $matthew -> lastName = &quot;Weier O'Phinney&quot; ; $matthew -> email = 'matthew@example.com' ; $matthew -> gender = 'male' ; $matthew -> occupation = 'PHP Developer' ; echo $matthew -> boast (); // Matthew Weier O'Phinney is a PHP Developer
  24. 24. Why bother?
  25. 25. It's good programming <ul><li>Scope and encapsulation
  26. 26. Re-use (both re-using a class by instantiating many objects, and class extension)
  27. 27. Type enforcement
  28. 28. Testability and maintenance </li></ul>
  29. 29. Some words on scope <ul><li>Methods have access to: </li><ul><li>Whatever is passed to them
  30. 30. Any class member variables, constants, or methods (with respect to visibility) </li></ul><li>Use “ $this-> ” within a class to access member variables and methods (unless statically declared); use “ -> ” when consuming an instance
  31. 31. Use “ self:: ” to access member constants </li></ul>
  32. 32. What is this “visibility” stuff? <ul><li>Determines scope in which a class member may be accessed. </li><ul><li>Public : can be accessed via instances, anywhere within the class definition, and by any extending class.
  33. 33. Protected : can be accessed within the class definition, and by extending classes.
  34. 34. Private : may only be accessed and modified within the defining class. </li></ul></ul>
  35. 35. Extension? <ul><li>Use the extends keyword: </li></ul>class Matthew extends Person “ Extends” keyword!
  36. 36. Extension: override properties class Matthew extends Person { public $firstName = 'Matthew' ; public $lastName = &quot;Weier O'Phinney&quot; ; public $occupation = 'PHP Developer' ; public $email = 'matthew@example.com' ; public $gender = 'male' ; }
  37. 37. Extension: override methods class Matthew extends Person { public function boast () { // get parent value from method $boast = parent :: boast (); $boast .= ', and wrote this presentation' ; return $boast ; } } Access the original method!
  38. 38. Extension: use the new class $matthew = new Matthew (); echo $matthew -> boast (); // Matthew Weier O'Phinney is a PHP Developer, // and wrote this presentation Note: using Matthew, not Person! boast() now says something new!
  39. 39. Abstraction <ul><li>A formal way of defining extension points
  40. 40. Two types: </li><ul><li>Interfaces
  41. 41. Abstract Classes </li></ul></ul>
  42. 42. Interfaces <ul><li>Defines a public contract for classes that implement it
  43. 43. Ensures consuming classes will always have specific methods available </li></ul>
  44. 44. A sample interface interface Animal { public function makeNoise (); public function eat ( $food ); } “ Interface” keyword!
  45. 45. Interfaces may extend other interfaces interface Collection extends Countable , IteratorAggregate { } Collection now inherits methods from Countable and IteratorAggregate
  46. 46. Implementing an interface means defining the interfaces methods class Family extends Order implements Collection { // defined by Countable public function count () {} // defined by IteratorAggregate public function getIterator () {} } “ implements ” keyword; this object follows that contract
  47. 47. Abstract Classes <ul><li>Sometimes it's useful to create a “base” set of functionality.
  48. 48. At the same time, you may want to leave certain details up to implementing classes. </li></ul>
  49. 49. Sample Abstract Class abstract class Person { public $firstName ; public $lastName ; public $email ; public $occupation ; public $gender ; public function speak () { /* implementation in here */ } // let extension define this, though: abstract public function eat ( $food ); } “ abstract” keyword; extending class must implement
  50. 50. Abstract classes can also implement interfaces abstract class Family extends Order implements Collection { }
  51. 51. Magic methods <ul><li>Always prefixed by “ __ ” (double underscore)
  52. 52. Some tie into various object states (initialization, destruction, cloning)
  53. 53. Some allow &quot;overloading&quot; the functionality of the object (set or get &quot;virtual&quot; members; call &quot;virtual&quot; methods; etc.) </li></ul>
  54. 54. Object initialization public function __construct ( $firstName , $lastName ) { $this -> firstName = $firstName ; $this -> lastName = $lastName ; } Magic method: constructor
  55. 55. And on the subjects of scope and inheritance... <ul><li>STATIC members: </li><ul><li>Class members that do not require an object instance in order to operate.
  56. 56. “Global” state (i.e., does not vary between instances); use rarely and wisely.
  57. 57. Uses the &quot;Paamayim Nekudotayim&quot; operator (` :: `) for scope resolution </li></ul></ul>
  58. 58. Static example class Registry { protected static $values = array (); public static function set ( $name , $value ) { static :: $values [ $name ] = $value ; } public static function get ( $name ) { if ( isset ( static :: $values [ $name ])) { return static :: $values [ $name ]; } return null ; } } Registry :: set ( 'foo' , 'bar' ); echo Registry :: get ( 'foo' ); // 'bar' The type is small because I don't want you to use statics. :)
  59. 59. Type validation <ul><li>PHP allows you to typehint on any class, abstract class, or interface name. </li></ul>if (! $matthew instanceof Person ) { echo &quot;Imposter!&quot; ; }
  60. 60. Parting remarks
  61. 61. Keep your objects discrete and concise <ul><li>All members should be related to a discrete subject
  62. 62. Methods should not do too much
  63. 63. Too many members often means you're mixing concerns, and should separate into more classes </li></ul>
  64. 64. Statics: don't do them <ul><li>Static functionality is appealing and seductive, but that way leads to the Dark Side </li></ul>
  65. 65. Magic methods <ul><li>Learn the magic methods, but don't lean to heavily on them </li><ul><li>Except in the case of __construct() , which is almost always a good idea. </li></ul></ul>
  66. 66. Learn the SPL <ul><li>Some SPL interfaces allow array-like access to objects, including iteration, array access to properties, etc.
  67. 67. Objects like ArrayObject, SplStack , and others can be huge time savers.
  68. 68. Solutions based off of SPL will almost always be faster than userland code. </li></ul>
  69. 69. Use OOP … … but don't forget your procedural roots; if a function will do, use it.
  70. 70. Resources <ul><li>http://thinkvitamin.com/author/lornajanemitchell/
  71. 71. http://php.net/oop
  72. 72. http://php.net/spl </li></ul>
  73. 73. Thank You! <ul><li>These slides are online: http://slidesha.re/bctnVk
  74. 74. Rate the talk! http://joind.in/2046 </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×