PHP - Design patterns

    There are a number of known design patterns that are available for the PHP language. Some of them are explained below-

    • Builder: How a composite object gets created
    • Factory Method: A subclass of an object that is instantiated
    • Singleton: The sole instance of a class

    1. Singleton

    This design pattern is very common and allows you to access only one single instance of the class and provide a global access point to that instance.

    Example:

    <?php
       class Singleton_demo {
          public static function data() {
             static $var = null;         
             if (null === $var) {
                $var = new static();
             }
             return $var;
          }
          protected function __construct() {
          }
          private function __clone() {
          }
          private function __wakeup() {
          }
       }
       class SingletonChild extends Singleton_demo {
       }
       $obj = Singleton_demo::getInstance();
       var_dump($obj === Singleton_demo::getInstance());
       $anotherObj = SingletonChild::data();
       var_dump($anotherObj === Singleton_demo::data());
       var_dump($anotherObj === SingletonChild::data()); 
    ?>

    Output:

    bool(true)
    bool(false)
    bool(true)

    2. Factory

    This design pattern works exactly as its name suggests. This pattern will work as the real factory ob the object instances. Suppose we know that a factory creates some types of product but we are not aware of how they do it but there is a universal way of doing it.

    Example:

    <?php
       class Subject {
          private $topic;
          private $teacher;
          public function __construct($topic_1, $teacher_1) {
             $this->topic = $topic_1;
             $this->teacher = $teacher_1;
          }
          public function getdata() {
             return $this->topic . ' ' . $this->teacher;
          }
       }
       class Subject_demo {
          public static function create($topic_1, $teacher_1) {
             return new Subject($topic_1, $teacher_1);
          }
       }
       $CS = Subject_demo::create(‘CS, ‘XXX’);
       print_r($CS>getdata());
       class Subject{
          private $topic;
          private $teacher;
          public function _construct($topic_1, $teacher_1) {
             $this->topic = $topic_1;
             $this->teacher = $teacher_1;
          }
          public function getdata() {
             return $this->topic . ' ' . $this->teacher;
          }
       }
       class Subject_Demo{
          public static function create($topic_1, $teacher_1) {
             return new Subject($topic_1, $teacher_1);
          }
       }
       $CS = Subject_demo::create(‘CS’, ‘XXX’);
       print_r($CS>getdata()); 
    ?>

    Output:

    CS XXX CS XXX

    3. Strategy

    This design pattern is an algorithm based where we encapsulate some algorithms and allow the client class to create an instance for a particular algorithm without having knowledge of the actual implementation.

    Example:

    <?php
       $elements_demo = array(
          array(
             'id' => 2,
             'name' => ‘XXX’,
          ),
          array(
             'id' => 1,
             ‘name’=> ‘YYY’
          )
       );
       $collection_demo = new ObjectCollection($elements_demo);
       $collection_demo->setComparator(new IdComparator());
       $collection_demo->sort();
       echo "Sorting by id:\n";
       print_r($collection_demo->elements_demo);
       $collection_demo->setComparator(new DateComparator());
       $collection_demo->sort();
       echo "Sortin by name:\n";
       print_r($collection_demo->elements_demo);
    ?>
    

    People are also reading: