The OOP in PHP

Matthew Turland

I Work Here

http://www.synacor.com

Synacor
Synacor Headquarters - Buffalo, NY

I Co/Wrote These

php|architect's Guide to Web Scraping with PHP
http://phparch.com
PHP Master: Write Cutting-Edge Code
http://sitepoint.com
The opening scene of Star Wars

Functions

xkcd #55: Useless

Lots of Functions

'Guns. Lots of guns.' scene from The Matrix

Lots of Guards Too

Or logs full of this:

PHP Notice:  Undefined index: alpha
PHP Notice:  Undefined index: bravo
PHP Notice:  Undefined index: delta

Scope

"In computer programming, a scope is the context within a computer program in which an identifier is in effect, and can be resolved to an entity – when the entity is visible."

http://en.wikipedia.org/wiki/Scope_(computer_science)

See also: Variable Scope in the PHP manual

Debugging Globals is Hard

Earth with a cartoon face on fire

Conclusion: We need a level of scope beyond the global namespace and individual function scope.

A Better Way

Objects!

Several geometrically-shaped objects

Encapsulation

"Encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)

A white box wrapped in red ribbon, topped with a bow

What's a Class?

See also: The Basics in the PHP manual

Blueprint of the USS Enterprise

Classes

Methods

Compare:
with:

Objects

Visibility

"Each person is three people in one: the person everyone sees, the person only close family and friends see, and the person only they see."

(Most likely paraphrased) Chinese proverb

See also: Visibility in the PHP manual

Jessica Alba as the Invisible Woman in the Avengers

Public Access Modifier

Entrance to a public library

Constants

See also: Constants in the PHP manual

Private Access Modifier

Be careful with these

Woman receiving a biometric eye scan

Inheritance

"Inheritance is a way to establish is-a relationships between objects."

http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

Chart showing the scientific classification of a grizzly bear

Subclasses

parent::getTotal() calls this method defined in this class.

See also: Object Inheritance in the PHP manual

Protected Access Modifier

A family home

Polymorphism

"The primary usage of polymorphism... is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior."

http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

Chart showing the scientific classification of a grizzly bear

Class Typehints

See also: Type Hinting in the PHP manual

Abstraction

"Abstraction is the process by which data and programs are defined with a representation similar in form to its meaning (semantics), while hiding away the implementation details... a concept or idea not associated with any specific instance."

http://en.wikipedia.org/wiki/Abstraction_(computer_science)

Yellow Curtain by Henri Matisse circa 1915

Abstract Classes

See also: Class Abstraction in the PHP manual

Design by Contract

"Design by contract... is an approach for designing software. It prescribes that software designers should define formal, precise and verifiable interface specifications for software components... These specifications are referred to as 'contracts,' in accordance with a conceptual metaphor with the conditions and obligations of business contracts."

http://en.wikipedia.org/wiki/Design_by_contract

Dilbert on contracts

Interfaces

See also: Object Interfaces in the PHP manual

ArrayAccess Interface

See also: ArrayAccess in the PHP manual

Iterator Interface

See also: Iterator in the PHP manual

Don't Repeat Yourself

"Don't Repeat Yourself (DRY) is a principle of software development aimed at reducing repetition of information of all kinds... is stated as 'Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.'"

http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

Don't Repeat Yourself: Repetition is the root of all software evil

Horizontal Reuse

"Multiple inheritance is a feature... in which a class can inherit... features from more than one superclass. It is distinct from single inheritance, where a class may only inherit from one particular superclass."

http://en.wikipedia.org/wiki/Multiple_inheritance

"A mixin is a class which contains a combination of methods from other classes... not by inheritance... Mixins encourage code reuse and avoid well-known pathologies associated with multiple inheritance.

http://en.wikipedia.org/wiki/Mixin

"Traits are a mechanism for code reuse... by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies... and avoids the typical problems associated with multiple inheritance and mixins.

http://www.php.net/manual/en/language.oop5.traits.php

"Don't over-complicate this: traits are compiler-assisted copy-and-paste."

Rasmus Lerdorf, almost definitely paraphrased

Traits

See also: Traits in the PHP manual and the horizontal reuse RFC

Constructors and Destructors

Composition

"Object composition... is a way to combine simple objects or data types into more complex ones."

http://en.wikipedia.org/wiki/Object_composition

"Composition over inheritance... is a technique by which classes may achieve polymorphic behavior and code reuse by containing [instances of] other classes that implement the desired functionality instead of through inheritance.

http://en.wikipedia.org/wiki/Composition_over_inheritance

The ogre Shref comparing ogres to onions, saying that both have layers

Dependency Injection

Dependency Injection Containers

More on this DIC implementation at pimple.sensiolabs.org

Autoloading

Example lifted from the PSR-0 standard

Check out this blog post on autoloading strategies

Classmaps

Use a script like this one to generate the classmap during your build process

Comparing Objects

See also: Comparing Objects

Cloning Objects

See also: Object Cloning and Objects and references in the PHP manual

Object Serialization

Code from the film The Matrix

See also: Object Serialization in the PHP manual

Exceptions

See also: Exceptions

Beyond This Point

Cover image for the film 'There Be Dragons'

"Do not meddle in the affairs of dragons for you are crunchy and taste good with ketchup"

Final

Absolutely, under no circumstances at all are customers allowed to play Stairway to Heaven, Sweet Child of Mine, or Smoke on the Water while trying out guitars. Thank you.

Statics

Be careful with these

Magic Methods

"Magic" has the same connotation as "interesting" in the Chinese proverb.

Use these sparingly, intentionally, and with great hesitation.

Corrupted Roots card from Magic: The Gathering

See also: Magic Methods in the PHP manual

Property Magic Methods

Instance Magic Methods

Static Magic Methods

Serialization Magic Methods

String Magic Methods

Invocation Magic Methods

Resources

SPL

Where to Find Me

Me at SunshinePHP 2013

Feedback

Please rate my talk!

https://joind.in/8731

Also, check out the joind.in mobile apps!

QR code to a page for submitting feedback for this talk