Top Banner
Object-Oriented Programming How to melt your brain with PHP Day 1
27

OOP Day 1

May 26, 2015

Download

Technology

Brian Fenton

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OOP Day 1

Object-Oriented Programming

How to melt your brain with PHP

Day 1

Page 2: OOP Day 1

Intro to OOP Concepts

• What are objects (classes)?• Properties• Methods

Page 3: OOP Day 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!

Page 4: OOP Day 1

Main Principles

• Information hiding• Abstraction• Segmentation• Limited responsibility

Page 5: OOP Day 1

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

Classes

• Defined with class ClassName{…}

• What is a class?• Bear!

Page 7: OOP Day 1

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

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

Object Methods

• Functions• What an object can “do”• Example Bear methods

– eat()– sleep()

Page 10: OOP Day 1

Questions thus far?

Page 11: OOP Day 1

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

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

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

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

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

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

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

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

Practical example: DateTime

• Encapsulates many of the date_* functions• DateTime::format()• DateTime::modify()• DateTime::setTimeZone()

– Requires a DateTimeZone object

Page 20: OOP Day 1

Question breakAnyone?

Page 21: OOP Day 1

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

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

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

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

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

You made it!Questions? Comments? Death threats?

Page 27: OOP Day 1

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/