This document discusses 5 common PHP design patterns: Singleton, Factory, Observer, Decorator, and Strategy. For each pattern, it provides a brief definition, examples of when it may be used, and code samples demonstrating its implementation in PHP. The document aims to introduce PHP developers to fundamental design patterns in an accessible way and encourage applying these patterns to improve code organization and reusability. It also stresses that design patterns are language-agnostic solutions to recurring problems and have been used by developers for many years, even if unfamiliar to some PHP programmers.
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"
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
13.
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.
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