Design Patterns in PHP

Design Patterns in PHP


Hello folks! welcome back to a new section of our tutorial on PHP. In this tutorial guide, we are going to be discussing about Design Patterns in PHP.

Microsoft design pattern theory is stated as follows: "The document introduces patterns and then presents them in a repository, or a catalogue, which is organized to help you in locating the right combinations of patterns that solves your problem".

Examples of Design Patterns

Singleton

A Class have only one instance, it provides a global access point to it. Following code will explain the Singleton concept -

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


The above example Implemented based on static method creation is getInstance().

Factory

A Class creates the object and you want to use that object. Following example below illustrates factory design pattern concept.

<?php
   class Automobile {
      private $bikeMake;
      private $bikeModel;
      
      public function __construct($make, $model) {
         $this->bikeMake = $make;
         $this->bikeModel = $model;
      }
      
      public function getMakeAndModel() {
         return $this->bikeMake . ' ' . $this->bikeModel;
      }
   }
   
   class AutomobileFactory {
      public static function create($make, $model) {
         return new Automobile($make, $model);
      }
   }
   
   $pulsar = AutomobileFactory::create('ktm', 'Pulsar');
   print_r($pulsar->getMakeAndModel());
   
   class Automobile {
      private $bikeMake;
      private $bikeModel;
      
      public function __construct($make, $model) {
         $this->bikeMake = $make;
         $this->bikeModel = $model;
      }
      
      public function getMakeAndModel() {
         return $this->bikeMake . ' ' . $this->bikeModel;
      }
   }
   
   class AutomobileFactory {
      public static function create($make, $model) {
         return new Automobile($make, $model);
      }
   }
   t$pulsar = AutomobileFactory::create('ktm', 'pulsar');
   
   print_r($pulsar->getMakeAndModel()); 
?>

The main difficulty with the factory pattern is that it increases code complexity, hence it is not reliable for good programmers.


Strategy Pattern

The Strategy Design Pattern makes family algorithm and encapsulate each algorithm. Each algorithm should be inter-changeable within the family.

<?php
   $elements = array(
      array(
         'id' => 2,
         'date' => '2020-10-17',
      ),
      array(
         'id' => 1,
         'date' => '2020-10-17'
      )
   );
   
   $collection = new ObjectCollection($elements);
   
   $collection->setComparator(new IdComparator());
   $collection->sort();
   
   echo "Sorted by ID:\n";
   print_r($collection->elements);
   
   $collection->setComparator(new DateComparator());
   $collection->sort();
   
   echo "Sorted by date:\n";
   print_r($collection->elements);
?>

Model View Control

View acts as GUI, while Model acts as the Back End and Control acts as an adapter. Here the three parts are linked with each other. It will pass and also access the data between each other.

Models view templates


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP Built-in Functions.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.