The OOP in PHP

Matthew Turland

Questions or Feedback?

Slides

Linked from either of these:

https://joind.in/15499

http://matthewturland.com/publications

I Work Here

When I Work

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

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

Terminology

What We Will Cover

Concepts

What We Will Cover

Principles

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

Variable

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

State

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

Function

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

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

See also: Variable Scope

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

"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

Objects

Classes

See also: The Basics

Classes

Instantiation

Inheritance

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

Chart showing the scientific classification of a grizzly bear

Subclasses

See also: Object Inheritance

Subclasses

Visibility

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

Constants

See also: Constants

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

Interfaces

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

Interfaces

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

Chart showing the scientific classification of a grizzly bear

Class Typehints

See also: Type Hinting

Class Typehints

Interface Typehints

Composition

"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

pimple.sensiolabs.org

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

Exceptions

See also: Exceptions

Final

See also: Final Keyword

Statics

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

Resources

Where to Find Me

Me with my cat Igor

Feedback

Please rate my talk!

https://joind.in/15499

Also, check out the joind.in mobile apps!