SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
1.
Object Oriented Design Patterns for PHP <?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?> An introduction of sorts into the world of design patterns for object oriented PHP, as brought to you by Robert Gonzalez. http://www.robert-gonzalez.com [email_address] http://www.robert-gonzalez.com/personal/meetups/apache-49-20081218-code.tar.gz http://www.robert-gonzalez.com/personal/meetups/apache-49-20081218-presentation.odp http://www.slideshare.net/RobertGonzalez/object-oriented-design-patterns-for-php-presentation/
2.
Object Oriented Design Patterns for PHP <ul><li>Who is this Robert dude? </li></ul><ul><li>What are design patterns? </li></ul><ul><li>How can design patterns help you? </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?> $this->setup();
3.
Object Oriented Design Patterns for PHP <ul><li>Who is this Robert dude? </li></ul><ul><li>Husband and father </li></ul><ul><li>Web developer </li></ul><ul><ul><li>Self taught </li></ul></ul><ul><ul><ul><li>Began learning HTML in 1997 </li></ul></ul></ul><ul><ul><ul><li>Began learning Perl in 2000 </li></ul></ul></ul><ul><ul><ul><li>Began learning PHP in 2003 </li></ul></ul></ul><ul><ul><li>Full time developer for Bay Alarm Company since 2006 </li></ul></ul><ul><li>PHP freak </li></ul><ul><ul><li>Administrator of the PHP Developers Network forums </li></ul></ul><ul><ul><li>Frequent contributor to Professional PHP Google group </li></ul></ul><ul><ul><li>Chief helper of n00Bs (at work and beyond) </li></ul></ul><ul><ul><li>Zend Certified PHP 5 Engineer </li></ul></ul><ul><li>Awesome barbecuer </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
4.
Object Oriented Design Patterns for PHP <ul><li>What are design patterns? </li></ul><ul><li>The words ”Object Oriented Design” repeated on wall paper </li></ul><ul><li>An industry buzzword that gets you into meetups </li></ul><ul><li>Recurring solutions to common software development challenges </li></ul><ul><li>Mind bending algorithms of confusion and indigestion </li></ul><ul><li>The words ”Object Oriented Design” repeated on wall paper </li></ul><ul><li>An industry buzzword that gets you into meetups </li></ul><ul><li>Recurring solutions to common software development challenges </li></ul><ul><li>Mind bending algorithms of confusion and indigestion </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
5.
Object Oriented Design Patterns for PHP <ul><li>What are design patterns not ? </li></ul><ul><li>They are not the end all, be all to all of your coding challenges. </li></ul><ul><li>They are not a requirement of object oriented programming. </li></ul><ul><li>They are not a requirement of good programming. </li></ul><ul><li>They are not a substitute for application code and logic. </li></ul><ul><li>They are not always the best solution for the job. </li></ul><ul><li>They are not always the easiest concept to grasp and/or use. </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
6.
Object Oriented Design Patterns for PHP <ul><li>Some common software development challenges: </li></ul><ul><li>Building new applications while leveraging existing codebases </li></ul><ul><li>Building/enhancing applications securely, quickly and effectively </li></ul><ul><li>Integrating new and existing applications </li></ul><ul><li>Multiple developers programming the same product </li></ul><ul><li>Few developers programming many products </li></ul><ul><li>Varying, sometimes unknown, data sources </li></ul><ul><li>Varying, sometimes unknown, operating systems </li></ul><ul><li>Varying, sometimes unknown, platforms and setups </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
7.
Object Oriented Design Patterns for PHP <ul><li>Yeah, this is all well and good. And your presentation so far kicks all kinds of ass, but seriously, what are design patterns really going to do for me? </li></ul><ul><li>Let's have a look at some patterns and see ... </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
8.
Object Oriented Design Patterns for PHP <ul><li>The Lazy Load Pattern </li></ul><ul><li>Lazy loading is the process of delaying the instantiation of an object until the instance is needed. </li></ul><ul><li>We all know the mantra, lazy programmers are the best programmers. </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
9.
Object Oriented Design Patterns for PHP <ul><li>The Lazy Load Pattern </li></ul><ul><li><?php /** * Sample class */ class MyClass { /** * Holder for the SomeObject object * * @access protected * @var SomeObject */ protected $_myobject = null ; /** * Get the instance of SomeObject, but only when we need it * * @access public * @return SomeObject */ public function fetchObject () { if ( $this -> _myobject === null ) { $this -> _myobject = new SomeObject ; } return $this -> _myobject ; } } </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
10.
Object Oriented Design Patterns for PHP <ul><li>Why the Lazy Load rocks: </li></ul><ul><li>Delays resource consumption until it is needed, which may be never </li></ul><ul><li>Is relatively easy to understand </li></ul><ul><li>Offers an encapsulated means of creating objects </li></ul><ul><li>Offers an encapsulated means of storing objects for use later </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?> <ul><ul><li>Did you just see that? Did he just say storing objects for use later? </li></ul></ul>
11.
Object Oriented Design Patterns for PHP <ul><li>The Registry Pattern </li></ul><ul><li>A registry is an object that other objects can use to access data, settings, values and other objects from a sort of internal storehouse. </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
12.
Object Oriented Design Patterns for PHP <ul><li>The Registry Pattern </li></ul><ul><li><?php /** * Class that manages registry entries for an application. */ class Registry { /** * Holds the registry labels and values data */ protected static $_register = array(); /** * Sets a value into the registry if there is no value with this label already */ public static function set ( $label , $value ) { if (!isset( self :: $_register [ $label ])) { self :: $_register [ $label ] = $value ; } } /** * Gets a value from the registry if there is a label with this value */ public static function get ( $label ) { if (isset( self :: $_register [ $label ]) { return self :: $_register [ $label ]; } } } </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
13.
Object Oriented Design Patterns for PHP <ul><li>Common uses of a Registry: </li></ul><ul><li>Maintaining values throughout a request </li></ul><ul><li>Setting/getting configuration values </li></ul><ul><li>Allowing access to variables within an application without globals </li></ul><ul><li>For Windows user, causing hours and hours of grief when corrupt </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?> <ul><ul><li>Can you think of a feature/extension of PHP that uses a sort of registry pattern? </li></ul></ul><ul><ul><li>Is there way to instantiate the Registry Pattern but force only a single instance of it? </li></ul></ul>
14.
Object Oriented Design Patterns for PHP <ul><li>The Singleton Pattern </li></ul><ul><li>The singleton patterns ensures that one and only one instance of an object exists. </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
15.
Object Oriented Design Patterns for PHP <ul><li>The Singleton Pattern </li></ul><ul><li><?php /** * Class that only ever has one instance of itself floating around. */ class Singleton { /** * Holds the instance of itself in this property */ private static $_instance = null ; /** * Final and private constructor means this can only be instantiated from within */ final private function __construct () {} /** * Gets the single instance of this object */ public static function getInstance () { if ( self :: $_instance === null ) { self :: $_instance = new self ; } return self :: $_instance ; } } </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
16.
Object Oriented Design Patterns for PHP <ul><li>Pros and cons of the Singleton: </li></ul><ul><li>Pro: You know you have the same instance available at all times. </li></ul><ul><li>Con: One and only one instance is all you get. </li></ul><ul><li>Pro: Instantiates itself for you so all you need to do is get it. </li></ul><ul><li>Con: Autoinstantiation diminishes flexibility of argument passing. </li></ul><ul><li>Pro: Since it is singleton it can replace global variable declaration. </li></ul><ul><li>Con: Since it is singleton it can replace global variable declaration. </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?> <ul><ul><li>What are some situations of a developer needing one and only one instance of an object? </li></ul></ul>
17.
Object Oriented Design Patterns for PHP <ul><li>The Factory Method Pattern </li></ul><ul><li>The factory method is a method whose sole purpose in life is to create objects. Most commonly (and really per definition) the factory method is an interface method that delegates object instantiation decisions to subclasses. However, it also commonly acceptable to say that a method that creates objects (both of known and unknown classes) is a factory method. </li></ul><ul><li>PLEASE NOTE: The factory method pattern is not the same as the factory pattern, which we will NOT be covering in this presentation. </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
18.
Object Oriented Design Patterns for PHP <ul><li>The Factory Method Pattern </li></ul><ul><li><?php /** * A factory method implementation */ abstract class Creator { /** * Abstract factory method to be defined by child classes */ abstract public function createObject ( $type ); } class ConcreteCreatorA extends Creator { public function createObject ( $type ) { switch ( $type ) { // Handle cases for concrete classes } } } class ConcreteCreatorB extends Creator { public function createObject ( $type ) { switch ( $type ) { // Handle cases for concrete classes } } } $b = new ConcreteCreatorB ; $b -> create ( 'ConcreteProductB' ); </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
19.
Object Oriented Design Patterns for PHP <ul><li>The Factory Method Pattern, an alternative </li></ul><ul><li><?php /** * A little class to handle creation of objects */ class Lib_Factory { /** * Create an object of type $name */ public static function createObject ( $name , $args = array()) { /** * Move our class name into a file path, turning names like * App_Db_Handler into something like App/Db/Handler.php */ $file = str_replace ( '_' , PATH_SEPARATOR , $name ) . '.php' ; // Get the file if ( file_exists ( $file )) { require_once $file ; $obj = new $name ( $args ); return $obj ; } </li></ul><ul><li> // Throw an exception if we get here throw new Exception ( "The class name $name could not be instantiated." ); } } </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
20.
Object Oriented Design Patterns for PHP <ul><li>The Factory Method Pattern benefits: </li></ul><ul><li>Encapsulates object creation </li></ul><ul><li>Allows a range of functionality for error trapping, loading, etc </li></ul><ul><li>Does not need to know anything about what it is creating </li></ul><ul><li>Easy to read, easy to understand </li></ul><ul><li>Lightweight, fast and can be made into a static method </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?> <ul><ul><li>Is there a way to tie all these patterns together into something usable? </li></ul></ul><ul><ul><li>I think we can, but first... </li></ul></ul>
21.
Object Oriented Design Patterns for PHP <ul><li>The Strategy Pattern </li></ul><ul><li>The strategy pattern defines and encapsulates a set of algorithms and makes them interchangeable, allowing the algorithms to vary independent from the objects that use them. </li></ul><ul><li>Uh, what? </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
22.
Object Oriented Design Patterns for PHP <ul><li>The Strategy Pattern </li></ul><ul><li>Encapsulates what changes and leaves what doesn't change alone </li></ul><ul><li>Makes encapsulated algorithms interchangeable </li></ul><ul><li>Favors composition over inheritance </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?> <ul><ul><li>With some patterns, like the Strategy Pattern, the best way to ”get” it is to see it. </li></ul></ul><ul><ul><li>Let's have a look... </li></ul></ul>
23.
Object Oriented Design Patterns for PHP <ul><li>The Strategy Pattern, part 1 </li></ul><ul><li><?php abstract class Computer { public $type ; public $platform ; abstract public function identify (); public function transport () { $this -> type -> transport (); } public function openTerminal () { $this -> platform -> terminal (); } public function closeTerminal () { echo "Close the terminal: type e-x-i-t, hit <enter>" ; } public function setComputerType ( ComputerType $type ) { $this -> type = $type ; } public function setPlatform ( ComputerPlatform $platform ) { $this -> platform = $platform ; } public function gotoNext () { echo "Moving on..." ; } } </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
24.
Object Oriented Design Patterns for PHP <ul><li>The Strategy Pattern, part 2 </li></ul><ul><li><?php interface ComputerType { public function transport (); } class ComputerType_Laptop implements ComputerType { public function transport () { echo "Transporting... Put the laptop in the bag and no one gets hurt." ; } } class ComputerType_Desktop implements ComputerType { public function transport () { echo "Transporting... Get the boxes, load it up, move it." ; } } class ComputerType_Server implements ComputerType { public function transport () { echo "Transporting... Seriously? Yeah, right. Transport this!" ; } } </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
25.
Object Oriented Design Patterns for PHP <ul><li>The Strategy Pattern, part 3 </li></ul><ul><li><?php interface ComputerPlatform { public function terminal (); } class ComputerPlatform_Mac implements ComputerPlatform { public function terminal () { echo "Open the terminal: Go to applications -> terminal." ; } } class ComputerPlatform_Ubuntu implements ComputerPlatform { public function terminal () { echo "Open the terminal: Go to applications -> accessories -> terminal." ; } } class ComputerPlatform_Windows implements ComputerPlatform { public function terminal () { echo "Open the terminal: I am not smart enough to have a terminal but ..." ; } } </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
26.
Object Oriented Design Patterns for PHP <ul><li>The Strategy Pattern, part 4 </li></ul><ul><li><?php class Computer_Mac extends Computer { public function __construct () { $this -> type = new ComputerType_Laptop ; $this -> platform = new ComputerPlatform_Mac ; } public function identify () { echo "I am a mac." ; } } class Computer_PC extends Computer { public function __construct () { $this -> type = new ComputerType_Desktop ; $this -> platform = new ComputerPlatform_Windows ; } public function identify () { echo "I'm a PC (or is that POS?)." ; } } </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
27.
Object Oriented Design Patterns for PHP <ul><li>The Strategy Pattern, part 4 continued </li></ul><ul><li><?php class Computer_Server extends Computer { public function __construct () { $this -> type = new ComputerType_Server ; $this -> platform = new ComputerPlatform_Ubuntu ; } public function identify () { echo "I am Ubuntu, one of a multitude of flavors of linux." ; } } </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
28.
Object Oriented Design Patterns for PHP <ul><li>The Strategy Pattern, part 5 </li></ul><ul><li><?php class Computer_Identifier { public function __construct () { $mac = new Computer_Mac ; $mac -> identify (); $mac -> openTerminal (); $mac -> closeTerminal (); $mac -> gotoNext (); </li></ul><ul><li> $pc = new Computer_PC ; $pc -> identify (); $pc -> openTerminal (); $pc -> closeTerminal (); $pc -> gotoNext (); </li></ul><ul><li> $linux = new Computer_Server ; $linux -> identify (); $linux -> transport (); } } </li></ul><ul><li>$comps = new Computer_Identifier ; </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
29.
Object Oriented Design Patterns for PHP <ul><li>The Strategy Pattern, in action </li></ul><ul><li>I am a mac. </li></ul><ul><li>Open the terminal: Go to applications -> terminal. </li></ul><ul><li>Close the terminal: type e-x-i-t, hit <enter> </li></ul><ul><li>Moving on... </li></ul><ul><li>I'm a PC (or is that POS?). </li></ul><ul><li>Open the terminal: I am not smart enough to have a terminal but you can go to start -> all programs -> accessories -> command prompt. </li></ul><ul><li>Close the terminal: type e-x-i-t, hit <enter> </li></ul><ul><li>Moving on... </li></ul><ul><li>I am Ubuntu, one of a multitude of flavors of linux. </li></ul><ul><li>Transporting... Seriously? Yeah, right. Transport this! </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
30.
Object Oriented Design Patterns for PHP <ul><li>Notes about the Strategy Pattern </li></ul><ul><li>Probably one of the most common patterns in OOP </li></ul><ul><li>One of the most powerful patterns in development </li></ul><ul><li>Opens a wealth of possibility when understood </li></ul><ul><li>Very flexible when favoring composition over inheritance </li></ul><ul><li>Encapsulates what changes to allow for greater extendability </li></ul><?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?>
31.
Object Oriented Design Patterns for PHP <?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?> Knowing what we know now, can you think of how these patterns can be applied to your applications to make for faster, more robust and cleaner application code? Maybe we can look at some code...
32.
Object Oriented Design Patterns for PHP <?php if ( $this ->try && ! $this ->succeed()) $this ->try++; /* Robert Gonzalez, 2008-12-18 */ ?> Design patterns are a great way to modularize your code and create reusable structures. But they mean nothing if they go unused. Go code something. Much more information about design patterns, reusable code and object oriented programming can be found by simply typing those keywords into the Google search box :) http://www.robert-gonzalez.com [email_address]