Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OOP - Day 1

Brian Fenton
September 15, 2009

OOP - Day 1

Part one of a two part tutorial on object-oriented programming in PHP that I gave to the KU Web Developer community

Brian Fenton

September 15, 2009
Tweet

More Decks by Brian Fenton

Other Decks in Programming

Transcript

  1. Advantages of OOP •  Modular •  Easy code re-use w/o

    copy & paste •  Layers mean less to understand at once •  Code is more likely to match design •  More self-documenting than procedural code •  Free code!
  2. Main differences in PHP5 (briefly) •  Objects were completely redone

    in PHP5 •  All objects are automatically passed by reference •  Constructors are named differently •  New visibility options •  A few other, less important things (RTM)
  3. Objects •  Objects are instances of classes •  Can have

    many objects per class •  $this! •  Access properties and methods with “->” – With parentheses at the end, it’s a method •  $this->sleep()! – No parentheses, it’s a property •  $this->status! •  Bears!
  4. Object Properties •  Properties are variables •  What an object

    “has” or “knows” •  Can be any type, even other objects •  Example Bear properties – gender! – color! – name! – weight ! – status!
  5. Object Methods •  Functions •  What an object can “do”

    •  Example Bear methods – eat()! – sleep()!
  6. Inheritance (pt. 1) •  Classes inherit properties/methods from their parents

    – Depends on visibility (next topic) •  Source of the “no copy/paste” code reuse
  7. Inheritance (pt. 2) •  Uses “extends” keyword – PolarBear (child) extends

    Bear (parent) •  Only one parent allowed, choose wisely •  Child classes can redo old stuff or add new things – PolarBear can swim()
  8. Visibility (pt. 1) •  Applies to properties and methods • 

    3 choices: – public – visible/editable inside and out – protected – visible inside class and descendents only – private – visible inside original class only •  Go for private to start, then protected if children need it too
  9. Visibility (pt. 2) •  Private/protected names often start with an

    underscore – Well-supported convention – Applies to properties and methods – Being phased out due to IDE support •  Example: Shy Bear is shy – Protecting Bear’s properties/methods – PolarBear needs them too
  10. Getters/Setters (pt. 1) •  How do we use properties that

    aren’t visible? •  Advantages – More formatting options – Clear, explicit interface – Allows for read-only properties (getter only) – Allows for “virtual” properties
  11. Getters/Setters (pt. 2) •  Disadvantages – More code to write/maintain • 

    But… a decent IDE can write it all for you •  Examples: –  $bear->getStatus() vs. $bear->status! –  $bear->setName(‘Steve’) vs. $bear->name = ‘Steve’!
  12. Constructors •  A “magic method” that runs when an object

    is created with the “new” keyword •  Accepts parameters passed to new! –  $bear = new Bear(‘Steve’);! •  In PHP5 these are named “__construct()” •  Best practice is to only use these to assign property values –  IDEs can generate these for you
  13. Destructors •  Much less commonly-used •  Runs automatically when the

    object goes out of scope or is forcibly “unset” •  Primarily used to close db connections – Make sure they’re not still in use, if shared •  __destruct()!
  14. Practical example: DateTime •  Encapsulates many of the date_* functions

    •  DateTime::format()! •  DateTime::modify()! •  DateTime::setTimeZone()! – Requires a DateTimeZone object
  15. Exercise (yes, there’s a quiz) •  Write your own class

    that extends DateTime •  Must have the following methods: – getMonth()! – getDayOfWeek()! – getYear()!
  16. Passing objects by reference •  What references are •  Automatic

    in PHP5 – Using & will actually reverse this and copy the full object – Main source of OOP speed boost in PHP5 •  Moral: don’t try to outsmart the compiler
  17. Class constants (pt. 1) •  Like regular PHP constants, just

    tied to a class – Uses const keyword instead of define()! – PHP 5.3 can use const outside of classes •  Can’t be changed in child classes •  Which is clearer? –  $result->fetch_array(MYSQL_ASSOC)! –  $result->fetch_array(1)!
  18. Class constants (pt. 2) •  Outside the class: ClassName::CONSTANT • 

    Inside: self::CONSTANT or parent::CONSTANT •  Examples: –  $bear = new Bear(‘Steve’, Bear::GENDER_MALE);! –  $this->_gender(self::GENDER_MALE);!
  19. Type Hinting •  PHP will enforce a param’s type for

    you •  Great feature, use it whenever you can •  Works for arrays and objects – See SplTypes on php.net for more •  IDEs use type hints for auto-complete •  Bear example (PoohBear will only eat() Honey)
  20. Resources •  http://www.php.net/ –  Try php.net/%function_name% for a shortcut • 

    http://devzone.zend.com/article/638-PHP-101- part-7-The-Bear-Necessities –  Source of blatant theft •  http://net.tutsplus.com/tutorials/php/object- oriented-php-for-beginners/