Design Patterns in PHP

Web Design Tutorialz
3 min readOct 30, 2020

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

Microsoft design pattern theory is stated — “The document introduces patterns and presents them in a repository, or a catalogue, which is organized to help you locate the right combination 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 and therefore it is not reliable for good programmers.

Strategy Pattern

The Strategy Pattern makes family algorithm and encapsulates each algorithm. Each of the algorithms 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

The 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.

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

Feel free to ask your questions where necessary and i 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.

Originally published at https://www.webdesigntutorialz.com.

--

--

Web Design Tutorialz

A tutorial blog that provides tutorials on Web Design, Programming, Java Technologies, Mobile App Development, Database, Machine Learning, etc.