The OOP in PHP

Matthew Turland

Questions or Feedback?


Linked from either of these:

I Work Here

When I Work

I Co/Wrote These

php|architect's Guide to Web Scraping with PHP
PHP Master: Write Cutting-Edge Code

What We Won't Cover

Basic programming concepts or PHP syntax

What We Won't Cover

Material overlapping with other talks

What We Will Cover

Why PHP has an object model

Or, at least, why I think it does

What We Will Cover


What We Will Cover


What We Will Cover


What We Will Cover

Potential Bonus Topics

What OOP Is Not

What OOP Is

"A language that doesn’t affect the way you think about programming, is not worth knowing."

~ Dr. Alan Perlis, "Epigrams on Programming" circa 1982

Back to Basics


"... a variable ... is a storage location and an associated symbolic name (an identifier) which contains some ... value. The variable name is the usual way to reference the stored value." ~ Wikipedia

See also: Variables

Back to Basics


"... a computer program stores data in variables, which represent storage locations in the computer's memory. The contents of these memory locations, at any given point in the program's execution, is called the program's state." ~ Wikipedia

Back to Basics


"... a subroutine is a sequence of program instructions that perform a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed … a subroutine may be called a ... function..." ~ Wikipedia

See also: Functions


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

See also: Variable Scope


Global and Function Scope

* There are some PHP-specific exceptions to this.

Problems with Global Scope

See also: Why global variables can be bad

Problems with Function Scope

A Better Solution


"Encapsulation is... a language construct that facilitates the bundling of data with the methods... operating on that data." ~ Wikipedia

It has another meaning, more on that in a bit



See also: The Basics




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

Chart showing the scientific classification of a grizzly bear


See also: Object Inheritance



""Encapsulation is... a language mechanism for restricting access to some of the object's components." ~ Wikipedia

See also: Visibility

Public Access Modifier

Protected Access Modifier

Private Access Modifier


See also: Constants


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

See also: Class Abstraction

Abstract Classes

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

Very important for extensibility and interoperability.


"... a[n]... interface is a common means for unrelated objects to communicate with each other. These are definitions of methods and values which the objects agree upon in order to cooperate." ~ Wikipedia
"When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck." ~ James Whitcomb Riley, the basis for duck typing

See also: Object Interfaces



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

Chart showing the scientific classification of a grizzly bear

Class Typehints

See also: Type Hinting

Class Typehints

Interface Typehints


"Object composition... is a way to combine simple objects or data types into more complex ones." ~ Wikipedia
"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." ~ Wikipedia

Onions have layer

Constructors / Destructors

"... a constructor in a class is a special type of subroutine … [that] prepares the new object for use, often accepting arguments that the constructor uses to set required member variables." ~ Wikipedia
"... a destructor... is a method which is automatically invoked when the object is destroyed... Its main purpose is to free the resources... which were acquired by the object along its life cycle and/or deregister from other entities which may keep references to it." ~ Wikipedia

Constructors / Destructors

Dependency Injection

"Dependency injection is a software design pattern... An injection is the passing of a dependency (a service) to a dependent object (a client)." ~ Wikipedia

Types of injection:

Types of injectors:

Setter Dependency

Constructor Dependency

Dependency Injection Containers

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

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

Separation of Concerns

"... separation of concerns (SoC) is a design principle for separating a computer program into distinct sections, such that each section addresses a separate concern... When concerns are well-separated, individual sections can be reused, as well as developed and updated independently." ~ Wikipedia

That's not how it works

Single Reponsibility Principle

"In object-oriented programming, the single responsibility principle states that every class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class." ~ Wikipedia

You had one job

Lagniappe Point

Comparing Objects

See also: Comparing Objects

Cloning Objects

See also: Object Cloning and Objects and References

Object Serialization

See also: Object Serialization


See also: Exceptions


See also: Final Keyword


See also: Static Keyword

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.

See also: Magic Methods

Property Magic Methods

Instance Magic Methods

Static Magic Methods

Serialization Magic Methods

String Magic Methods

Invocation Magic Methods


Where to Find Me

Me with my cat Igor


Please rate my talk!

Also, check out the mobile apps!