oop day 1
DESCRIPTION
Day one of a two day seminar on object-oriented PHP. Designed to cover a basic level of familiarity with the concepts in general, and some PHP-specific implementation details and examples.TRANSCRIPT
![Page 1: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/1.jpg)
Object-Oriented Programming
How to melt your brain with PHP
Day 1
![Page 2: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/2.jpg)
Intro to OOP Concepts
• What are objects (classes)?• Properties• Methods
![Page 3: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/3.jpg)
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!
![Page 4: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/4.jpg)
Main Principles
• Information hiding• Abstraction• Segmentation• Limited responsibility
![Page 5: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/5.jpg)
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)
![Page 6: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/6.jpg)
Classes
• Defined with class ClassName{…}
• What is a class?• Bear!
![Page 7: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/7.jpg)
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!
![Page 8: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/8.jpg)
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
![Page 9: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/9.jpg)
Object Methods
• Functions• What an object can “do”• Example Bear methods
– eat()– sleep()
![Page 10: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/10.jpg)
Questions thus far?
![Page 11: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/11.jpg)
Inheritance (pt. 1)
• Classes inherit properties/methods from their parents– Depends on visibility (next topic)
• Source of the “no copy/paste” code reuse
![Page 12: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/12.jpg)
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()
![Page 13: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/13.jpg)
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
![Page 14: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/14.jpg)
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
![Page 15: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/15.jpg)
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
![Page 16: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/16.jpg)
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’
![Page 17: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/17.jpg)
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
![Page 18: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/18.jpg)
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()
![Page 19: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/19.jpg)
Practical example: DateTime
• Encapsulates many of the date_* functions• DateTime::format()• DateTime::modify()• DateTime::setTimeZone()
– Requires a DateTimeZone object
![Page 20: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/20.jpg)
Question breakAnyone?
![Page 21: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/21.jpg)
Exercise (yes, there’s a quiz)
• Write your own class that extends DateTime
• Must have the following methods:– getMonth()– getDayOfWeek()– getYear()
![Page 22: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/22.jpg)
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
![Page 23: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/23.jpg)
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)
![Page 24: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/24.jpg)
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);
![Page 25: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/25.jpg)
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)
![Page 26: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/26.jpg)
You made it!Questions? Comments? Death threats?
![Page 27: OOP Day 1](https://reader035.vdocuments.us/reader035/viewer/2022062418/556381f5d8b42acc128b461e/html5/thumbnails/27.jpg)
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/