OOP Day 1
-
Upload
brian-fenton -
Category
Technology
-
view
1.191 -
download
0
description
Transcript of OOP Day 1
Object-Oriented Programming
How to melt your brain with PHP
Day 1
Intro to OOP Concepts
• What are objects (classes)?• Properties• Methods
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!
Main Principles
• Information hiding• Abstraction• Segmentation• Limited responsibility
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)
Classes
• Defined with class ClassName{…}
• What is a class?• Bear!
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!
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
Object Methods
• Functions• What an object can “do”• Example Bear methods
– eat()– sleep()
Questions thus far?
Inheritance (pt. 1)
• Classes inherit properties/methods from their parents– Depends on visibility (next topic)
• Source of the “no copy/paste” code reuse
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()
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
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
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
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’
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
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()
Practical example: DateTime
• Encapsulates many of the date_* functions• DateTime::format()• DateTime::modify()• DateTime::setTimeZone()
– Requires a DateTimeZone object
Question breakAnyone?
Exercise (yes, there’s a quiz)
• Write your own class that extends DateTime
• Must have the following methods:– getMonth()– getDayOfWeek()– getYear()
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
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)
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);
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)
You made it!Questions? Comments? Death threats?
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/