Basic PHP Design Patterns

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    14 Favorites

    Basic PHP Design Patterns - Presentation Transcript

    1. Basic PHP Design Patterns
      • Stefan Priebsch, e-novative GmbH
    2.  
    3.  
    4.  
    5. „ Every pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over,without ever doing it the same way twice.“ (Christopher Alexander)
    6. Software Design Patterns Are
      • Blueprints for proven solutions
      • A common language for developers
      • Useful to understand frameworks
    7. They Are Not
      • The solution to all design problems
      • Ready-made implementations
      • Magic bullets
    8. Map
    9. An object that maps keys to values. A map cannot contain duplicate keys.
    10. Map
      • class Map { }
    11. Map
      • class Map { protected $values = array(); }
    12. Map
      • class Map { protected $values = array(); public function set($aKey, $aValue) { return $this->values[$aKey] = $aValue; } }
    13. Map
      • class Map { protected $values = array(); public function set($aKey, $aValue) { return $this->values[$aKey] = $aValue; } public function get($aKey) { return $this->values[$aKey]; } }
    14. Map
      • class Map { protected $values = array(); public function __construct() { $this->values = array('ip' => '192.168.1.1'); } public function set($aKey, $aValue) { return $this->values[$aKey] = $aValue; } public function get($aKey) { return $this->values[$aKey]; } }
    15. Using the Map
      • $map = new Map(); var_dump($map->get('ip')); $map->set('ip', '192.168.1.2'); var_dump($map->get('ip'));
    16. Using the Map
      • $map = new Map(); var_dump($map->get('ip')); $map->set('ip', '192.168.1.2'); var_dump($map->get('ip')); > php Map.php string(11) "192.168.1.1" string(11) "192.168.1.2"
    17. Using the Map
      • $map = new Map(); var_dump($map->get('nonexisting key'));
    18. Using the Map
      • $map = new Map(); var_dump($map->get('nonexisting key')); > php Map.php Notice: Undefined index: nonexisting key in Map.php on line 21 NULL
    19. Map
      • class Map { protected $values = array(); public function __construct() ... public function set($aKey, $aValue) ... public function get($aKey) { if (!isset($this->values[$aKey])) { throw new RuntimeException('No key "' . $aKey . '"'); } return $this->values[$aKey]; } }
    20. Map
      • class Map { protected $values = array(); public function __construct() ... public function set($aKey, $aValue) ... public function has($aKey) { return isset($this->values[$aKey]); } public function get($aKey) ... }
    21. Extending the Map
      • class MyMap extends Map { public function __construct() { // load values from ini or XML file, database, or PHP data } }
    22. Map Data Loader
      • class MapDataLoader { protected $source; public function __construct($aSource) { $this->source = $aSource; } }
    23. Map Data Loader
      • class MapDataLoader { protected $source; public function __construct($aSource) { $this->source = $aSource; } public function load() { // load data from $this->source return $result; // e.g. array('key' => 'value', ...) } }
    24. Map using Data Loader
      • class MyMap { public function __construct( MapDataLoader $aLoader) { $this->values = $aLoader->load(); } }
    25. Registry
    26. A well-known object that other objects can use to find common objects and services.
    27. Registry
      • class Registry { protected $dsn; public function setDsn($aDsn) { $this->dsn = $aDsn; } public function getDsn() { return $this->dsn; } }
    28. Registry
      • class Registry { protected $dsn; protected $username; protected $password; public function setDsn($aDsn) ... public function getDsn() ... public function setUsername() {} public function getUsername() {} public function setPassword() {} public function getPassword() {} }
    29. Registry
      • $registry = new Registry(); $registry->setDsn('sqlite::memory:'); var_dump($registry->getDsn());
    30. Registry
      • $registry = new Registry(); $registry->setDsn('sqlite::memory:'); var_dump($registry->getDsn()); > php Registry.php string(15) "sqlite::memory:"
    31. Singleton
    32. Singleton is used to restrict instantiation of a class to one object.
    33. Singleton
      • class Singleton { }
    34. Singleton
      • class Singleton { protected static $instance; }
    35. Singleton
      • class Singleton { protected static $instance; public static function getInstance() { return self::$instance; } }
    36. Singleton
      • class Singleton { protected static $instance; public static function getInstance() { return self::$instance; } } $singleton = Singleton::getInstance();
    37. Singleton
      • class Singleton { protected static $instance; public static function getInstance() { if (is_null(self::$instance)) { self::$instance = new Singleton(); } return self::$instance; } } $singleton = Singleton::getInstance();
    38. Singleton
      • class Singleton { protected static $instance; private function __construct() {} public static function getInstance() { if (is_null(self::$instance)) { self::$instance = new Singleton(); } return self::$instance; } } $singleton = Singleton::getInstance();
    39. Singleton
      • class Singleton { protected static $instance; private function __construct() {} private function __clone() {} public static function getInstance() { if (is_null(self::$instance)) { self::$instance = new Singleton(); } return self::$instance; } } $singleton = Singleton::getInstance();
    40. Singleton
      • class Singleton { protected static $instance; private function __construct() {} private function __clone() {} public static function getInstance() { if (is_null(self::$instance)) { self::$instance = new Singleton(); } return self::$instance; } } $singleton = Singleton::getInstance();
    41. Subject/Observer
    42. The observer maintains a list of its dependents and notifies them automatically of any state changes, usually by calling one of their methods.
    43. Subject/Observer Interfaces (available through SPL)
      • interface SplSubject { public function attach (SplObserver $aObserver); public function detach (SplObserver $aObserver); public function notify (); }
      • interface SplObserver { public function update (SplSubject $aSubject); }
    44. Subject
      • class Subject implements SplSubject { protected $observers = array(); }
    45. Subject
      • class Subject implements SplSubject { protected $observers = array(); public function attach(SplObserver $aObserver) { $this->observers[] = $aObserver; } }
    46. Subject
      • class Subject implements SplSubject { protected $observers = array(); public function attach(SplObserver $aObserver) { $this->observers[] = $aObserver; } public function detach(SplObserver $aObserver) { $this->observers[] = array_diff($this->observers, array($aObserver)); } }
    47. Subject
      • class Subject implements SplSubject { protected $observers = array(); public function attach(SplObserver $aObserver) ... public function detach(SplObserver $aObserver) ... public function notify() { foreach ($this->observers as $observer) { $observer->update($this); } } }
    48. Subject
      • class Subject implements SplSubject { protected $observers = array(); public function attach(SplObserver $aObserver) ... public function detach(SplObserver $aObserver) ... public function notify() ... public function doSomething() { // Do some work $this->notify(); } }
    49. Observer
      • class Observer implements SplObserver { public function update(SplSubject $aSubject) { var_dump($this, 'updated'); } }
    50. Using Subject/Observer
      • $subject = new Subject(); $observer = new Observer(); $observer2 = new Observer(); $subject->attach($observer); $subject->attach($observer2); $subject->doSomething();
    51. Using Subject/Observer
      • $subject = new Subject(); $observer = new Observer(); $observer2 = new Observer(); $subject->attach($observer); $subject->attach($observer2); $subject->doSomething(); > php Subject_Observer.php object(Observer)#2 (0) { } string(7) "updated" object(Observer)#3 (0) { } string(7) "updated"
    52. Iterator
    53. An iterator allows traversing through all elements of a collection. Iterators encapsulate the internal structure of how the iteration occurs.
    54. Iterator Interface
      • interface Iterator { // Returns the current value function current(); // Returns the current key function key(); // Moves the internal pointer to the next element function next(); // Moves the internal pointer to the first element function rewind(); // If the current element is valid (boolean) function valid(); }
    55. SPL Iterators in PHP
      • Iterator, CachingIterator, FilterIterator, ArrayIterator, DirectoryIterator, RecursiveDirectoryIterator, SimpleXMLiterator
      • Documentation at http://www.php.net/~helly
    56. Live Demo
    57. Literature
    58. Literature
      • Stephan Schmidt: „PHP Design Patterns“
      • Matt Zandstra: „PHP 5 Objects, Patterns, and Practise“
      • Fowler: „Patterns of Enterprise Application Architecture“
      • Joshua Kerievsky: „Refactoring to Patterns“
    59. Thank you.
    60. http://www.priebsch.de http://www.e-novative.de [email_address] Profiles at Xing, LinkedIn, Facebook
    61.  

    + Stefan PriebschStefan Priebsch, 2 years ago

    custom

    3174 views, 14 favs, 4 embeds more stats

    Basic design patterns that help you create better P more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 3174
      • 3150 on SlideShare
      • 24 from embeds
    • Comments 0
    • Favorites 14
    • Downloads 0
    Most viewed embeds
    • 21 views on http://blog.rafaelcaceres.net
    • 1 views on http://rafaelcaceres.blogspot.com
    • 1 views on http://phpshore.blogspot.com
    • 1 views on http://lamp.jiecn.net

    more

    All embeds
    • 21 views on http://blog.rafaelcaceres.net
    • 1 views on http://rafaelcaceres.blogspot.com
    • 1 views on http://phpshore.blogspot.com
    • 1 views on http://lamp.jiecn.net

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories