SlideShare a Scribd company logo
Your First 5 PHP Design Patterns
           by Aaron Saray
Your first 5 PHP design patterns - ThatConference 2012
Why trust this guy?
                  ● 20 years of
                    programming

                  ● MKEPUG

                  ● I wrote the book on
                    this topic

                  ● ... you paid! ;)
Obligatory Slide
● Web Development
  Manager
  ○ Liturgical Publications, Inc.
What we're here to do...
● Talk about what Design Patterns are
    ○ you don't need a comp-sci phd

●   Show you where you've seen them before

●   Introduce 5 design patterns
    ○ real-people-talk descriptions
    ○ code samples

● Q&A
What are Patterns?
What are Design Patterns?
● Solving the same problem over and over
  ○ but the 'right' way

  ○ the old guys got it right

● Why is this important to PHP programmers?
  ○ PHP is easy to get into

  ○ We think we'll save the world, or own it?
    ■ FB is worth billions - and is PHP right?
Software Design has Three Parts
● "What"
  ○ business and functionality

● "How"
  ○ which design you choose

● "Work"
  ○ implementation - or using "how"
Where do Design Patterns fall?
Design Patterns are the "How"




                  They make the "work"
                  less sucky.
This isn't new...
● Non-PHP Programmers have seen this before
  ○ I vaguely remember something from college JAVA

● PHP Guys, you've seen this before
  ○ PEAR DB
  ○ Zend Registry
  ○ Zend URI Factory
  ○ Doctrine DAO
The Godfathers
● Gang of Four
  ○ Erich Gamma
  ○ Richard Helm
  ○ Ralph Johnson
  ○ John Vlissides
Your first 5 PHP design patterns - ThatConference 2012
What Design Patterns are NOT
● Not Plug n Play code

● You can't blindly copy these

● Unproven theory
  ○ Design Laws?
So, before we go on...
● Solving the same problem over and over
  ○ correctly

● Language agnostic
  ○ but we're going to focus on PHP

● These are not new - you've used them

● GoF

● Not Copy and Paste
Let's do this!
#1 - Singleton Pattern
(purists, stop groaning)



● The Singleton Design Pattern is used to restrict the
       number of times a specific object can be created to a
       single time by providing access to a shared instance of
       itself.

● Why might you use this?
  ○ Db
  ○ Heavy Front Build
  ○ Static Content
#1 continued
● Sometimes 1 instance isn't right...
  ○ could restrict to 5 instances
    ■ pool?

● Was someone next to you groaning?
  ○ "A proper registry of objects do away with this
      pattern. Mrrr mRrrr mrrr! Rabble Rabble!"

   ○ programmers aren't responsible.
Singleton Code
Singleton Code
or... with Abstracts too...
There are many ways to skin a cat...

wait.. what? Poor cat...
Singleton Code
Singleton Code
#2 - Factory Pattern
● The Factory Design Pattern provides a simple interface
   to acquire a new instance of an object while sheltering
   the calling code from the steps to determine which base
   class is actually instantiated.

● Why might we use this?
  ○ Inventory system dealing with types of objects
  ○ View system requesting objects
  ○ Youtube vs Vimeo URL
Factory Code
Factory Code
#3 - Observer Pattern
● The Observer Design pattern facilitates the creation of
   objects that watch the state of a targeted functionality
   that is uncoupled from the core object.

● Why might we use this
  ○ Plugins
  ○ Don't want to modify shared code
  ○ Licensing
  ○ Enable / Disable auxiliary functions
Observer Code
Observer Code
Observer Code
Observer Code
#4 - Decorator Pattern
● The Decorator Design Pattern is best suited for altering
   or "decorating" portions of an existing object's content or
   functionality without modifying the structure of the
   original object.

● Why might we use this?
  ○ quick small changes to internal content / values
  ○ modify user input (filter)
  ○ pretty output
Decorator Code
Decorator Code
Decorator Code
#5 - Strategy Pattern
● The Strategy Design Pattern helps architect an object
   that can make use of algorithms in other objects on
   demand in lieu of containing the logic itself.

● Why might we use this?
  ○ reduce code duplication on similar models
  ○ no re-inventing the wheel
  ○ quickly add a different process without changing the
      base object
      ■ Ex: YouTube changes the URL format
Strategy Code
Strategy Code
Strategy Code
What's next?
● Put this into practice

● Refactor your code / Plan new code

● Learn more design patterns:
  ○ GoF: http://saray.me/KqqHm1
  ○ Me: http://saray.me/JIP98B
Aaron Saray
                         Open Source Developer
                         Milwaukee, WI
Questions?
                             http://aaronsaray.com

● Questions about            @aaronsaray
  PHP Design Patterns?


                         Milwaukee PHP Users Group
                         http://mkepug.org
                         @mkepug

More Related Content

PDF
Design patterns in PHP - PHP TEAM
PDF
Common design patterns in php
PDF
Enterprise PHP: mappers, models and services
PDF
Everyday Functional Programming in JavaScript
PDF
Everyday Functional Programming in JavaScript
PDF
Good code, Bad Code
PPTX
F# for startups
Design patterns in PHP - PHP TEAM
Common design patterns in php
Enterprise PHP: mappers, models and services
Everyday Functional Programming in JavaScript
Everyday Functional Programming in JavaScript
Good code, Bad Code
F# for startups

What's hot (20)

ODP
Presentatie xPage & Beer
PPT
How to write bad code using C#
PPTX
Clean code coding like a professional
PDF
Prototyping Workshop - Wireframes, Mockups, Prototypes
ODP
Java interfaces design perspective
ODP
BDD with Behat
PPTX
Behat - human-readable automated testing
PDF
Boston Startup School - OO Design
PPTX
Webdev bootcamp
PDF
Language portfolio
PDF
Web application
ODP
Geecon10: Object Oriented for nonbelievers
PPTX
Clean code, Feb 2012
PPTX
Templates: Where Should Its Elements Be? In the Template or on Another Module?
PPTX
Javascript 01 (js)
PDF
Metaprogramming Go
PPT
Web development basics (Part-3)
PPTX
Jak aspekty uporządkują twój kod.
PDF
Visual testing using PhantomCss
PPTX
Php traits
Presentatie xPage & Beer
How to write bad code using C#
Clean code coding like a professional
Prototyping Workshop - Wireframes, Mockups, Prototypes
Java interfaces design perspective
BDD with Behat
Behat - human-readable automated testing
Boston Startup School - OO Design
Webdev bootcamp
Language portfolio
Web application
Geecon10: Object Oriented for nonbelievers
Clean code, Feb 2012
Templates: Where Should Its Elements Be? In the Template or on Another Module?
Javascript 01 (js)
Metaprogramming Go
Web development basics (Part-3)
Jak aspekty uporządkują twój kod.
Visual testing using PhantomCss
Php traits
Ad

Viewers also liked (17)

PPTX
25 php interview questions – codementor
PDF
Design patterns in PHP
PDF
Design patterns revisited with PHP 5.3
ODP
Object Oriented Design Patterns for PHP
PDF
Design Patterns & JDK Examples
PDF
PHP client - Mongo db User Group Pune
PDF
Zend Php Certification Study Guide
PPT
Zend PHP 5.3 Demo Certification Test
PPT
Introduction to Design Patterns and Singleton
PDF
Practice exam php
DOCX
1000+ php questions
PPTX
Design pattern (Abstract Factory & Singleton)
PDF
Top 100 PHP Interview Questions and Answers
DOCX
Business analysis interview question and answers
PDF
Zend Certification Preparation Tutorial
PDF
85 business analyst interview questions and answers
PDF
Basic design & visual arts (Elements of design)
25 php interview questions – codementor
Design patterns in PHP
Design patterns revisited with PHP 5.3
Object Oriented Design Patterns for PHP
Design Patterns & JDK Examples
PHP client - Mongo db User Group Pune
Zend Php Certification Study Guide
Zend PHP 5.3 Demo Certification Test
Introduction to Design Patterns and Singleton
Practice exam php
1000+ php questions
Design pattern (Abstract Factory & Singleton)
Top 100 PHP Interview Questions and Answers
Business analysis interview question and answers
Zend Certification Preparation Tutorial
85 business analyst interview questions and answers
Basic design & visual arts (Elements of design)
Ad

Similar to Your first 5 PHP design patterns - ThatConference 2012 (20)

PDF
Xlab #2: wzorce projektowe
PDF
Everyone hacks design at a hackathon
PDF
Bdd agile requirements
PDF
Meetup 18/10/2018 - Artificiële intelligentie en mobiliteit
PDF
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
PPTX
Create your library
PDF
Professional development
PDF
Fundamentals of App & Web Design
PDF
Coding dojo
PPTX
2024.04 - AI in Code Generation - April User Group Meeting
PPTX
AI in Code Generation - March User Group
PDF
WordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress Coding
PDF
Getting started contributing to Apache Spark
PDF
Open source, What | Why | How
PPTX
Software Engineering Primer
PPTX
Failing the coding interview
PDF
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
PDF
Why no one is looking for rockstar programmers (updated version)
PDF
Design of everyday things fundamental principles of interaction - V2
PDF
Sharpening the Axe
Xlab #2: wzorce projektowe
Everyone hacks design at a hackathon
Bdd agile requirements
Meetup 18/10/2018 - Artificiële intelligentie en mobiliteit
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Create your library
Professional development
Fundamentals of App & Web Design
Coding dojo
2024.04 - AI in Code Generation - April User Group Meeting
AI in Code Generation - March User Group
WordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress Coding
Getting started contributing to Apache Spark
Open source, What | Why | How
Software Engineering Primer
Failing the coding interview
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Why no one is looking for rockstar programmers (updated version)
Design of everyday things fundamental principles of interaction - V2
Sharpening the Axe

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PPTX
MYSQL Presentation for SQL database connectivity
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Advanced Soft Computing BINUS July 2025.pdf
Big Data Technologies - Introduction.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
MYSQL Presentation for SQL database connectivity
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Understanding_Digital_Forensics_Presentation.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Modernizing your data center with Dell and AMD
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
madgavkar20181017ppt McKinsey Presentation.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Electronic commerce courselecture one. Pdf
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Advanced Soft Computing BINUS July 2025.pdf

Your first 5 PHP design patterns - ThatConference 2012

  • 1. Your First 5 PHP Design Patterns by Aaron Saray
  • 3. Why trust this guy? ● 20 years of programming ● MKEPUG ● I wrote the book on this topic ● ... you paid! ;)
  • 4. Obligatory Slide ● Web Development Manager ○ Liturgical Publications, Inc.
  • 5. What we're here to do... ● Talk about what Design Patterns are ○ you don't need a comp-sci phd ● Show you where you've seen them before ● Introduce 5 design patterns ○ real-people-talk descriptions ○ code samples ● Q&A
  • 7. What are Design Patterns? ● Solving the same problem over and over ○ but the 'right' way ○ the old guys got it right ● Why is this important to PHP programmers? ○ PHP is easy to get into ○ We think we'll save the world, or own it? ■ FB is worth billions - and is PHP right?
  • 8. Software Design has Three Parts ● "What" ○ business and functionality ● "How" ○ which design you choose ● "Work" ○ implementation - or using "how"
  • 9. Where do Design Patterns fall?
  • 10. Design Patterns are the "How" They make the "work" less sucky.
  • 11. This isn't new... ● Non-PHP Programmers have seen this before ○ I vaguely remember something from college JAVA ● PHP Guys, you've seen this before ○ PEAR DB ○ Zend Registry ○ Zend URI Factory ○ Doctrine DAO
  • 12. The Godfathers ● Gang of Four ○ Erich Gamma ○ Richard Helm ○ Ralph Johnson ○ John Vlissides
  • 14. What Design Patterns are NOT ● Not Plug n Play code ● You can't blindly copy these ● Unproven theory ○ Design Laws?
  • 15. So, before we go on... ● Solving the same problem over and over ○ correctly ● Language agnostic ○ but we're going to focus on PHP ● These are not new - you've used them ● GoF ● Not Copy and Paste
  • 17. #1 - Singleton Pattern (purists, stop groaning) ● The Singleton Design Pattern is used to restrict the number of times a specific object can be created to a single time by providing access to a shared instance of itself. ● Why might you use this? ○ Db ○ Heavy Front Build ○ Static Content
  • 18. #1 continued ● Sometimes 1 instance isn't right... ○ could restrict to 5 instances ■ pool? ● Was someone next to you groaning? ○ "A proper registry of objects do away with this pattern. Mrrr mRrrr mrrr! Rabble Rabble!" ○ programmers aren't responsible.
  • 21. or... with Abstracts too... There are many ways to skin a cat... wait.. what? Poor cat...
  • 24. #2 - Factory Pattern ● The Factory Design Pattern provides a simple interface to acquire a new instance of an object while sheltering the calling code from the steps to determine which base class is actually instantiated. ● Why might we use this? ○ Inventory system dealing with types of objects ○ View system requesting objects ○ Youtube vs Vimeo URL
  • 27. #3 - Observer Pattern ● The Observer Design pattern facilitates the creation of objects that watch the state of a targeted functionality that is uncoupled from the core object. ● Why might we use this ○ Plugins ○ Don't want to modify shared code ○ Licensing ○ Enable / Disable auxiliary functions
  • 32. #4 - Decorator Pattern ● The Decorator Design Pattern is best suited for altering or "decorating" portions of an existing object's content or functionality without modifying the structure of the original object. ● Why might we use this? ○ quick small changes to internal content / values ○ modify user input (filter) ○ pretty output
  • 36. #5 - Strategy Pattern ● The Strategy Design Pattern helps architect an object that can make use of algorithms in other objects on demand in lieu of containing the logic itself. ● Why might we use this? ○ reduce code duplication on similar models ○ no re-inventing the wheel ○ quickly add a different process without changing the base object ■ Ex: YouTube changes the URL format
  • 40. What's next? ● Put this into practice ● Refactor your code / Plan new code ● Learn more design patterns: ○ GoF: http://saray.me/KqqHm1 ○ Me: http://saray.me/JIP98B
  • 41. Aaron Saray Open Source Developer Milwaukee, WI Questions? http://aaronsaray.com ● Questions about @aaronsaray PHP Design Patterns? Milwaukee PHP Users Group http://mkepug.org @mkepug