Introduction to PHP Design Patterns

Introduction to PHP Design Patterns

Introduction to PHP Design Patterns

Introduction to PHP Design Pattern

What are Design Patterns?

  • Solution to common development problems
  • Reusable successful design and architectures
  • Common language for developers

What Design Patterns are not?

  • The panacea for solving all development problems
  • Ready to use code scripts
  • Cut & Paste solutions

Why Design Patterns?

  • Help you to reuse successful design and architectures
  • Help you to choose web design alternatives if click this over here now
  • Help you to solve problems you haven’t seen before
  • Give a common vocabulary
  • Let you communicate quickly and unambiguously
  • Make a system more reusable

One of those magical performance cum code reduction issues is “Design Pattern” which was introduced by Eric Gamma and his three other friends in the book Design Patterns in 1972. Because of four authors, the book was introduced as written by Gang of Four or simply Goff. In that legendary book, Gang of Four introduced several patterns to minimize the amount of code as well as to introduce effective coding practice. In this chapter we will learn some of those patterns to implement in PHP.

The Gang of Four

  1. Erich Gamma
  2. Richard Helm
  3. Ralph Johnson
  4. John Vlissides

Essential Elements

Pattern name: A descriptive name for the pattern, in a word or two

Problem: Describes the problem the pattern applies to


¨       Describes the elements and the resources along with their relationship and responsibilities for solving the problem.

¨       This is just an abstract description; the implementation varies according to the programming language

Consequences: Results and trade-off

 Golden Rules

  1. Program to an interface, not an implementation
  2. Some languages takes this rule to the next level with Duck Typing
  3. Favor Object Composition over Inheritance
  4. Black Box approach
  5. Delegate

 How to Select a Design Pattern

  • Consider how to design patterns solve design problems
  • Scan Intent section
  • Study how patterns interrelate
  • Study patterns of like purpose
  • Examine a cause of redesign
  • Consider what should be variable in your design

How to Use a Design Pattern

  • Read the pattern once through for an overview
  • Go back and study the Structure, Participants and Collaboration sections
  • Look at the Sample Code section to see a concrete example of the pattern in code
  • Choose names for pattern participants that are meaningful in the application context
  • Define the classes
  • Define application-specific names for operations in the pattern
  • Implement the operations to carry out the responsibilities and collaborations in the patterns

How to NOT Use a Design Pattern

  • Don’t try to rewrite your application to fit a Design Pattern, select the best Pattern according to your needs
  • Don’t use a Design Pattern because it’s cool… use a Pattern because you need it
  • Don’t copy/paste language-specific Pattern implementations, understand how the Pattern works and how you can implement it in your application

Frameworks vs Design Patterns

  • Design patterns are more abstract than frameworks
  • Design patterns are smaller architectural elements than frameworks
  • Design patterns are less specialized than frameworks
  • A typical framework contains several design patterns, but the reverse is never true

Design Pattern Classification

Design Patterns by Purpose

  • Creational Patterns

Concern the process of object creation

  • Structural Patterns

Deal with the composition of object and classes

  • Behavioral Patterns

Characterize the way in which classes or objects interact

  • Concurrency Patterns
  • Architectural Patterns



Design Patterns by Scope

Class Patterns

  • Deal with relationships between classes and their subclasses.

Ex. Factory Method, Adapter, Template method…

Object Patterns

  • Deal with object relationships.
  • The most part of patterns are in the object scope.

Ex. Singleton, Adapter, Decorator, Proxy, Iterator, Observer…


Design Patterns


  • MVC


  • Singleton
  • Factory Method
  • Lazy initialization


  • Adapter
  • Proxy


  • Iterator
  • Observer
  • Strategy
  • Template Method


  • Registry
  • Mock Object

  Design Patterns in Action

Pattern Template

  • Problem & Solution
  • Considerations
  • In pictures
  • Implementation & Examples
  • In the Wild
  • Using and Abusing
4 comments to “Introduction to PHP Design Patterns”
  1. Hi,
    Would you like to add some code to demonstrate where/how and which to use design patterns? Like processing orders (shop), validating forms/input data, fascade for complex system with multiple models/tables….?

Leave a Reply

Your email address will not be published. Required fields are marked *