Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Design patterns in PHP - PHP TEAM

2,137 views

Published on

Covers some of the Design patterns and some common ways to re-factor code. Also, how to make things in much better way, which writing your code.

  • Be the first to comment

Design patterns in PHP - PHP TEAM

  1. 1. <?php Design Patterns In PHP Nishant Shrivastava @n1shant
  2. 2. •It is a solution to a problem. •Got a problem, chances are another DEV already has solved it. What is Design Pattern?
  3. 3. Why...? ● Improve Readability ● Improve Maintainability ● Gain better understanding of code
  4. 4. Understanding Patterns..
  5. 5. Let me rephrase it! Understanding Problem Patterns...
  6. 6. Understanding Patterns... ● Problem Patterns ● Architectural patterns ● Smell in Code
  7. 7. Solutions... Refactoring CONTEXT PROBLEM SOLUTION
  8. 8. D.R.Y Do not repeat Yourself!
  9. 9. K.I.S.S Keep.It.Simple.&.Stupid
  10. 10. Y.A.G.N.I You ain't gonna need it!
  11. 11. Patterns... ● Factory ● Singleton ● Delegate ● Decorator ● Strategy ● Observer ● Adaptor ● State ● Iterator ● Front Controller ● MVC ● Active Record
  12. 12. Factory Pattern ● Creates Objects without having to instantiate the classes directly. ● Factories creates Objects. ● When... ● Keep DRY when complex object creation needs to be re-usable. ● Encapsulate several Object creation in one.
  13. 13. Example : Factory Class Factory { public function create() { return new Thing(); } } Class Thing { public function doSomething() { echo “Hello PHP'ers!”; } } $factory = new Factory(); $thingObj = $factory->create(); $thing->doSomething();
  14. 14. Singleton Pattern ● Creates Objects without direct instantiation. ● Does not allow more than one instance of self ● Ensure only one instance of an obejct at any given time. ● When... ● Require only one instance of an class. ● Need only one connection to a server.
  15. 15. Example : Singleton Class SomeSingleton { public static $instance; private function __construct(){} private function __clone(){} public static function getInstance(){ If(!self::$instance instanceof self) { self::$instance = new self(); } Return self::$instance; } } ---------------------------------------------------------------------------------------- $someStaticInstance = SomeSingleton::getInstance(); $someInstance = new SomeSingleton();
  16. 16. Decorator Pattern ● Decorator adds functionality to an object without changing the Object's behavior. ● When... ● Add features or methods to an object, that are not part of the Core logic. ● Need extended functionality for specific use cases.
  17. 17. Example : Decorator class User{ Public $firstName = ''; Public $lastName = ''; } Class UserDecorator{ Private $user; Public function __construct(User $user){ $this->user = $user; } Public function getFullName() { Return “{$this->user->firstName} {$this->user->lastName}”; } }
  18. 18. Example : Decorator... $user = new User(); $user->firstName = 'Chuck'; $user->firstName = 'Norris'; $decoratedUser = new UserDecorator($user); echo $decoratedUser->getFullName(); // 'Chuck Norris'
  19. 19. Observer Pattern ● Objects(Subject) register other objects (Observer) that react to state changes of their subject. ● In simple words, Observer looks for changes and do something l When... ● State Changes of an Object affect other objects or datasets ● Event Handling ● Data persistence ● Logging
  20. 20. Example : Observer
  21. 21. ?>
  22. 22. Thank You! Nishant Shrivastava @n1shant

×