Your first 5 PHP design patterns - ThatConference 2012
Your First 5 PHP Design Patterns by Aaron Saray
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 were here to do...● Talk about what Design Patterns are ○ you dont need a comp-sci phd● Show you where youve seen them before● Introduce 5 design patterns ○ real-people-talk descriptions ○ code samples● Q&A
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 well 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"
Design Patterns are the "How" They make the "work" less sucky.
This isnt new...● Non-PHP Programmers have seen this before ○ I vaguely remember something from college JAVA● PHP Guys, youve 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
What Design Patterns are NOT● Not Plug n Play code● You cant blindly copy these● Unproven theory ○ Design Laws?
So, before we go on...● Solving the same problem over and over ○ correctly● Language agnostic ○ but were going to focus on PHP● These are not new - youve used them● GoF● Not Copy and Paste
#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 isnt 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 arent responsible.
#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
#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 ○ Dont want to modify shared code ○ Licensing ○ Enable / Disable auxiliary functions
#4 - Decorator Pattern● The Decorator Design Pattern is best suited for altering or "decorating" portions of an existing objects 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
#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