Object Oriented PHP: An Introduction

  • 4,062 views
Uploaded on

Overview of OOP basics in PHP.

Overview of OOP basics in PHP.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,062
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
208
Comments
0
Likes
6

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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