SlideShare a Scribd company logo
Common PHP Design
Patterns
David Stockton
Front Range PHP User Group - Feb 18, 2015
What are design patterns?
Common Language
Gang of Four
Cartman, Kyle, Stan, Kenny
Gang of Four
Gamma, Helm, Johnson, Vlissides
What aren’t design patterns?
Silver Bullets
Legos for Coding
Scratch is Legos
for Coding
This talk is not about scratch
Design Patterns
Help You Talk to
Other Coders
Hey, I’m building a chunk
of code that is used to
build an object which has
a lot of dependencies and
setup and each of those
has a few of their own so
this piece of code will take
care of putting together all
those pieces so that when
I need the object I just
have to ask this one object
to give me the other one
and then I can use it where
ever I need instead of
having to build a huge
dependency tree before
using this code

LOL
WUT?
I’m building a factory.
What does a factory do?
‱ Builds objects
‱ Encapsulates building of objects so there’s only one
place to ïŹnd how to build it
Common design patterns in php
Singleton
Singleton
There can be only one
Singleton
‱ For classes where we want only one instance
Common design patterns in php
Downsides of Singleton
‱ It is a global
‱ Hard to test
‱ Better ways to do this
Another pattern description
‱ I’m going to create a shopping cart where I can
choose how the discounts or promo codes will be
applied by injecting various objects, each of which can
include how to apply these discounts; for example
apply discounts only to items that don’t have other
discounts, apply discount percentage on the base
price not counting already applied discounts, apply
discount percentage after other applied discounts,
apply a ïŹ‚at amount of a discount for each item, apply
a discount on an item based on what type of item it is,
etc
Strategy pattern
‱ I’m going to create a shopping cart where I can
choose how the discounts or promo codes will be
applied by injecting various objects, each of which can
include how to apply these discounts; for example
apply discounts only to items that don’t have other
discounts, apply discount percentage on the base
price not counting already applied discounts, apply
discount percentage after other applied discounts,
apply a ïŹ‚at amount of a discount for each item, apply
a discount on an item based on what type of item it is,
etc
Strategy pattern
‱ Allow injection of object that provides some aspect of
the functionality
‱ Algorithm’s functionality can be decided at run-time
‱ Examples:
‱ usort - Provide comparison part of sort
‱ Car - brakingStrategy
Command Pattern
‱ I’m doing a bunch of stuff that I want to be able to
keep track of and potentially save and replay later,
possibly be able to undo. In order for this to work my
object will have to capture everything it needs to know
about as far as parameters and what-not in order to be
able to run later as well as potentially capturing state
so I can get back to where I was before.
Command Pattern
‱ Uses:
‱ Recording Macros
‱ Multi-level Undo
‱ Progress Bars
‱ Transactional Behavior
‱ Wizards
Chain of Responsibility
‱ Each object can take care of something (or not), then it
passes to the next object
Chain of Responsibility
Examples
‱ Loggers - set next logger in chain, each calls the next
when it is done
‱ Managers with purchasing power - if amount is above
manager’s level, pass to the next object in the chain
Observer Pattern
‱ Notify interested objects of state changes in subject
‱ Interested objects subscribe to subject
‱ When subject changes, it calls update on all observers
MVC - Model View Controller
‱ Organizational pattern for web dev
‱ View code contains output
‱ Model code is for data/business logic
‱ Controller links model and view code
Front Controller
‱ Central entry point for handling requests for web
application
‱ Route all calls through index.php, then ïŹgure out what
to do with it
Adapter Pattern
‱ Allow interface for an existing class to be used from
another interface
Facade Pattern
‱ Provide simpliïŹed access to a complex system
Other Patterns
‱ Bridge
‱ Composite
‱ Decorator
‱ Front Controller
‱ Blackboard
‱ Iterator
‱ Mediator
‱ Memento
‱ Null Object
‱ SpeciïŹcation
‱ State
‱ Template Method
‱ Visitor
‱ Abstract Factory
‱ Prototype
Common design patterns in php

More Related Content

PDF
Your first 5 PHP design patterns - ThatConference 2012
PDF
Design patterns in PHP - PHP TEAM
PPTX
25 php interview questions – codementor
PDF
Enterprise PHP: mappers, models and services
PPTX
Clean code
PDF
Good code, Bad Code
PPTX
Software development best practices & coding guidelines
Your first 5 PHP design patterns - ThatConference 2012
Design patterns in PHP - PHP TEAM
25 php interview questions – codementor
Enterprise PHP: mappers, models and services
Clean code
Good code, Bad Code
Software development best practices & coding guidelines

What's hot (20)

PDF
How To Become A Good C# Programmer
PPT
How to write bad code using C#
PPTX
Clean code coding like a professional
PPT
Clean Code summary
PPTX
Coding standards and guidelines
PPTX
Clean code, Feb 2012
 
PPSX
Coding standard
PPTX
Coding standards for java
PPT
Best Practices of Software Development
PPTX
Reactive GUI Implemented in Clojure
ODP
CLEAN CODE
ODP
Geecon10: Object Oriented for nonbelievers
PPTX
Software development fundamentals
PPTX
Writing code for others
PPTX
Clean Code
PPTX
Design pattern in an expressive language java script
PDF
Introduction to Frontend Development - Session 1 - HTML Fundamentals
PPT
Php
PPTX
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir
PDF
Why coding convention ?
How To Become A Good C# Programmer
How to write bad code using C#
Clean code coding like a professional
Clean Code summary
Coding standards and guidelines
Clean code, Feb 2012
 
Coding standard
Coding standards for java
Best Practices of Software Development
Reactive GUI Implemented in Clojure
CLEAN CODE
Geecon10: Object Oriented for nonbelievers
Software development fundamentals
Writing code for others
Clean Code
Design pattern in an expressive language java script
Introduction to Frontend Development - Session 1 - HTML Fundamentals
Php
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir
Why coding convention ?
Ad

Viewers also liked (6)

PDF
Design patterns in PHP
PDF
Design patterns revisited with PHP 5.3
ODP
Object Oriented Design Patterns for PHP
PPT
Introduction to Design Patterns and Singleton
PPTX
Design pattern (Abstract Factory & Singleton)
PDF
Design Patterns & JDK Examples
Design patterns in PHP
Design patterns revisited with PHP 5.3
Object Oriented Design Patterns for PHP
Introduction to Design Patterns and Singleton
Design pattern (Abstract Factory & Singleton)
Design Patterns & JDK Examples
Ad

Similar to Common design patterns in php (20)

PPTX
Design p atterns
PPTX
Software Design principales
PPTX
Orchestration, the conductor's score
PPTX
Segue to design patterns
PPTX
Weekly Meeting: Basic Design Pattern
KEY
Driving application development through behavior driven development
PPTX
SF Architect Interview questions v1.3.pptx
PPTX
Kaseya Connect 2013: Templates and Policy: The Next Steps
 
PPTX
Principled And Clean Coding
PPTX
Episode 3 – Classes, Inheritance, Abstract Class, and Interfaces
PPTX
How to crack java script certification
PDF
Unit Testing and role of Test doubles
PPTX
Clean code
PDF
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
PDF
An Introduction To Software Development - Final Review
PPTX
jForce In Action
PDF
Create Your Own Starter Files
PPTX
CPAN Curation
PPTX
Refactoring Legacy Code - true story
PDF
Best practices with development of enterprise-scale SharePoint solutions - Pa...
Design p atterns
Software Design principales
Orchestration, the conductor's score
Segue to design patterns
Weekly Meeting: Basic Design Pattern
Driving application development through behavior driven development
SF Architect Interview questions v1.3.pptx
Kaseya Connect 2013: Templates and Policy: The Next Steps
 
Principled And Clean Coding
Episode 3 – Classes, Inheritance, Abstract Class, and Interfaces
How to crack java script certification
Unit Testing and role of Test doubles
Clean code
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
An Introduction To Software Development - Final Review
jForce In Action
Create Your Own Starter Files
CPAN Curation
Refactoring Legacy Code - true story
Best practices with development of enterprise-scale SharePoint solutions - Pa...

More from David Stockton (19)

PDF
Phone calls and sms from php
PDF
The Art of Transduction
PDF
Using queues and offline processing to help speed up your application
PDF
Intermediate OOP in PHP
PDF
Building APIs with Apigilty and Zend Framework 2
PDF
API All the Things!
PDF
Intermediate OOP in PHP
PDF
Hacking sites for fun and profit
PDF
Beginning OOP in PHP
PDF
Intermediate oop in php
PDF
Grokking regex
PDF
Hacking sites for fun and profit
PDF
Hacking sites for fun and profit
PDF
Increasing code quality with code reviews (poetry version)
PPT
Tame Your Build And Deployment Process With Hudson, PHPUnit, and SSH
ZIP
Mercurial Distributed Version Control
PPTX
Regular expressions and php
PPTX
PHP 5 Magic Methods
PDF
FireBug And FirePHP
Phone calls and sms from php
The Art of Transduction
Using queues and offline processing to help speed up your application
Intermediate OOP in PHP
Building APIs with Apigilty and Zend Framework 2
API All the Things!
Intermediate OOP in PHP
Hacking sites for fun and profit
Beginning OOP in PHP
Intermediate oop in php
Grokking regex
Hacking sites for fun and profit
Hacking sites for fun and profit
Increasing code quality with code reviews (poetry version)
Tame Your Build And Deployment Process With Hudson, PHPUnit, and SSH
Mercurial Distributed Version Control
Regular expressions and php
PHP 5 Magic Methods
FireBug And FirePHP

Recently uploaded (20)

PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
PDF
Software Development Methodologies in 2025
 
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
This slide provides an overview Technology
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
PPTX
Comunidade Salesforce SĂŁo Paulo - Desmistificando o Omnistudio (Vlocity)
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Top Generative AI Tools for Patent Drafting in 2025.pdf
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
SparkLabs Primer on Artificial Intelligence 2025
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
PDF
Why Endpoint Security Is Critical in a Remote Work Era?
PDF
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Reimagining Insurance: Connected Data for Confident Decisions.pdf
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Software Development Methodologies in 2025
 
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
Chapter 2 Digital Image Fundamentals.pdf
madgavkar20181017ppt McKinsey Presentation.pdf
Smarter Business Operations Powered by IoT Remote Monitoring
This slide provides an overview Technology
A Day in the Life of Location Data - Turning Where into How.pdf
Comunidade Salesforce SĂŁo Paulo - Desmistificando o Omnistudio (Vlocity)
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Top Generative AI Tools for Patent Drafting in 2025.pdf
ChatGPT's Deck on The Enduring Legacy of Fax Machines
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
SparkLabs Primer on Artificial Intelligence 2025
NewMind AI Weekly Chronicles - July'25 - Week IV
Why Endpoint Security Is Critical in a Remote Work Era?
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
Telecom Fraud Prevention Guide | Hyperlink InfoSystem

Common design patterns in php

  • 1. Common PHP Design Patterns David Stockton Front Range PHP User Group - Feb 18, 2015
  • 2. What are design patterns?
  • 4. Gang of Four Cartman, Kyle, Stan, Kenny
  • 5. Gang of Four Gamma, Helm, Johnson, Vlissides
  • 9. Scratch is Legos for Coding This talk is not about scratch
  • 10. Design Patterns Help You Talk to Other Coders
  • 11. Hey, I’m building a chunk of code that is used to build an object which has a lot of dependencies and setup and each of those has a few of their own so this piece of code will take care of putting together all those pieces so that when I need the object I just have to ask this one object to give me the other one and then I can use it where ever I need instead of having to build a huge dependency tree before using this code

  • 14. What does a factory do? ‱ Builds objects ‱ Encapsulates building of objects so there’s only one place to ïŹnd how to build it
  • 18. Singleton ‱ For classes where we want only one instance
  • 20. Downsides of Singleton ‱ It is a global ‱ Hard to test ‱ Better ways to do this
  • 21. Another pattern description ‱ I’m going to create a shopping cart where I can choose how the discounts or promo codes will be applied by injecting various objects, each of which can include how to apply these discounts; for example apply discounts only to items that don’t have other discounts, apply discount percentage on the base price not counting already applied discounts, apply discount percentage after other applied discounts, apply a ïŹ‚at amount of a discount for each item, apply a discount on an item based on what type of item it is, etc
  • 22. Strategy pattern ‱ I’m going to create a shopping cart where I can choose how the discounts or promo codes will be applied by injecting various objects, each of which can include how to apply these discounts; for example apply discounts only to items that don’t have other discounts, apply discount percentage on the base price not counting already applied discounts, apply discount percentage after other applied discounts, apply a ïŹ‚at amount of a discount for each item, apply a discount on an item based on what type of item it is, etc
  • 23. Strategy pattern ‱ Allow injection of object that provides some aspect of the functionality ‱ Algorithm’s functionality can be decided at run-time ‱ Examples: ‱ usort - Provide comparison part of sort ‱ Car - brakingStrategy
  • 24. Command Pattern ‱ I’m doing a bunch of stuff that I want to be able to keep track of and potentially save and replay later, possibly be able to undo. In order for this to work my object will have to capture everything it needs to know about as far as parameters and what-not in order to be able to run later as well as potentially capturing state so I can get back to where I was before.
  • 25. Command Pattern ‱ Uses: ‱ Recording Macros ‱ Multi-level Undo ‱ Progress Bars ‱ Transactional Behavior ‱ Wizards
  • 26. Chain of Responsibility ‱ Each object can take care of something (or not), then it passes to the next object
  • 27. Chain of Responsibility Examples ‱ Loggers - set next logger in chain, each calls the next when it is done ‱ Managers with purchasing power - if amount is above manager’s level, pass to the next object in the chain
  • 28. Observer Pattern ‱ Notify interested objects of state changes in subject ‱ Interested objects subscribe to subject ‱ When subject changes, it calls update on all observers
  • 29. MVC - Model View Controller ‱ Organizational pattern for web dev ‱ View code contains output ‱ Model code is for data/business logic ‱ Controller links model and view code
  • 30. Front Controller ‱ Central entry point for handling requests for web application ‱ Route all calls through index.php, then ïŹgure out what to do with it
  • 31. Adapter Pattern ‱ Allow interface for an existing class to be used from another interface
  • 32. Facade Pattern ‱ Provide simpliïŹed access to a complex system
  • 33. Other Patterns ‱ Bridge ‱ Composite ‱ Decorator ‱ Front Controller ‱ Blackboard ‱ Iterator ‱ Mediator ‱ Memento ‱ Null Object ‱ SpeciïŹcation ‱ State ‱ Template Method ‱ Visitor ‱ Abstract Factory ‱ Prototype