The OOP in PHP

Matthew Turland

I Work Here

Synacor Headquarters - Buffalo, NY

I Co/Wrote These

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


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


"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."

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


Several geometrically-shaped objects


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

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






"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


See also: Constants in the PHP manual

Private Access Modifier

Be careful with these

Woman receiving a biometric eye scan


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

Chart showing the scientific classification of a grizzly bear


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

See also: Object Inheritance in the PHP manual

Protected Access Modifier

A family home


"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."

Chart showing the scientific classification of a grizzly bear

Class Typehints

See also: Type Hinting in the PHP manual


"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."

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."

Dilbert on contracts


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.'"

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."

"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.

"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.

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

Rasmus Lerdorf, almost definitely paraphrased


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

Constructors and Destructors


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

"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.

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

Dependency Injection

Dependency Injection Containers

More on this DIC implementation at


Example lifted from the PSR-0 standard

Check out this blog post on autoloading strategies


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


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"


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.


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



Where to Find Me

Me at SunshinePHP 2013


Please rate my talk!

Also, check out the mobile apps!

QR code to a page for submitting feedback for this talk