Your SlideShare is downloading. ×
0
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Object Oriented PHP: An Introduction
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Object Oriented PHP: An Introduction

4,353

Published on

Overview of OOP basics in PHP.

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,353
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
236
Comments
0
Likes
9
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&apos;s this guy, then? <ul><li>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 </li></ul>
  • 7. Who Are You? <ul><li>How many of you have used OOP before?
  • 8. How many of you use OOP regularly?
  • 9. If you&apos;re not using OOP, why are you interested? </li></ul>
  • 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 , &apos;foo&apos; ); define ( BAR , &apos;bar&apos; ); $value = &apos;bar&apos; ; switch ( $value ) { case FOO : // do something; break ; case BAR : // do something else break ; }
  • 14. Associative Arrays $person = array ( &apos;last_name&apos; =&gt; &amp;quot;Weier O&apos;Phinney&amp;quot; , &apos;first_name&apos; =&gt; &amp;quot;Matthew&amp;quot; , &apos;occupation&apos; =&gt; &apos;PHP Developer&apos; , &apos;gender&apos; =&gt; &apos;Male&apos; , &apos;email&apos; =&gt; &apos;matthew@example,com&apos; , );
  • 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 <ul><li>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 </li></ul>
  • 22. define methods, which are functions inside a class class Person { // ... attributes ... public function boast () { return sprintf ( &apos;%s %s is a %s&apos; , $this -&gt; firstName , $this -&gt; lastName , $this -&gt; occupation ); } } Instance variables!
  • 23. Use your objects! $matthew = new Person (); $matthew -&gt; firstName = &apos;Matthew&apos; ; $matthew -&gt; lastName = &amp;quot;Weier O&apos;Phinney&amp;quot; ; $matthew -&gt; email = &apos;matthew@example.com&apos; ; $matthew -&gt; gender = &apos;male&apos; ; $matthew -&gt; occupation = &apos;PHP Developer&apos; ; echo $matthew -&gt; boast (); // Matthew Weier O&apos;Phinney is a PHP Developer
  • 24. Why bother?
  • 25. It&apos;s good programming <ul><li>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 </li></ul>
  • 29. Some words on scope <ul><li>Methods have access to: </li><ul><li>Whatever is passed to them
  • 30. Any class member variables, constants, or methods (with respect to visibility) </li></ul><li>Use “ $this-&gt; ” within a class to access member variables and methods (unless statically declared); use “ -&gt; ” when consuming an instance
  • 31. Use “ self:: ” to access member constants </li></ul>
  • 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. Protected : can be accessed within the class definition, and by extending classes.
  • 34. Private : may only be accessed and modified within the defining class. </li></ul></ul>
  • 35. Extension? <ul><li>Use the extends keyword: </li></ul>class Matthew extends Person “ Extends” keyword!
  • 36. Extension: override properties class Matthew extends Person { public $firstName = &apos;Matthew&apos; ; public $lastName = &amp;quot;Weier O&apos;Phinney&amp;quot; ; public $occupation = &apos;PHP Developer&apos; ; public $email = &apos;matthew@example.com&apos; ; public $gender = &apos;male&apos; ; }
  • 37. Extension: override methods class Matthew extends Person { public function boast () { // get parent value from method $boast = parent :: boast (); $boast .= &apos;, and wrote this presentation&apos; ; return $boast ; } } Access the original method!
  • 38. Extension: use the new class $matthew = new Matthew (); echo $matthew -&gt; boast (); // Matthew Weier O&apos;Phinney is a PHP Developer, // and wrote this presentation Note: using Matthew, not Person! boast() now says something new!
  • 39. Abstraction <ul><li>A formal way of defining extension points
  • 40. Two types: </li><ul><li>Interfaces
  • 41. Abstract Classes </li></ul></ul>
  • 42. Interfaces <ul><li>Defines a public contract for classes that implement it
  • 43. Ensures consuming classes will always have specific methods available </li></ul>
  • 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 <ul><li>Sometimes it&apos;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. </li></ul>
  • 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 <ul><li>Always prefixed by “ __ ” (double underscore)
  • 52. Some tie into various object states (initialization, destruction, cloning)
  • 53. Some allow &amp;quot;overloading&amp;quot; the functionality of the object (set or get &amp;quot;virtual&amp;quot; members; call &amp;quot;virtual&amp;quot; methods; etc.) </li></ul>
  • 54. Object initialization public function __construct ( $firstName , $lastName ) { $this -&gt; firstName = $firstName ; $this -&gt; lastName = $lastName ; } Magic method: constructor
  • 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. “Global” state (i.e., does not vary between instances); use rarely and wisely.
  • 57. Uses the &amp;quot;Paamayim Nekudotayim&amp;quot; operator (` :: `) for scope resolution </li></ul></ul>
  • 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 ( &apos;foo&apos; , &apos;bar&apos; ); echo Registry :: get ( &apos;foo&apos; ); // &apos;bar&apos; The type is small because I don&apos;t want you to use statics. :)
  • 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 &amp;quot;Imposter!&amp;quot; ; }
  • 60. Parting remarks
  • 61. Keep your objects discrete and concise <ul><li>All members should be related to a discrete subject
  • 62. Methods should not do too much
  • 63. Too many members often means you&apos;re mixing concerns, and should separate into more classes </li></ul>
  • 64. Statics: don&apos;t do them <ul><li>Static functionality is appealing and seductive, but that way leads to the Dark Side </li></ul>
  • 65. Magic methods <ul><li>Learn the magic methods, but don&apos;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. Learn the SPL <ul><li>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. </li></ul>
  • 69. Use OOP … … but don&apos;t forget your procedural roots; if a function will do, use it.
  • 70. Resources <ul><li>http://thinkvitamin.com/author/lornajanemitchell/
  • 71. http://php.net/oop
  • 72. http://php.net/spl </li></ul>
  • 73. Thank You! <ul><li>These slides are online: http://slidesha.re/bctnVk
  • 74. Rate the talk! http://joind.in/2046 </li></ul>

×