How To Implement Design Patterns in PHP?

Published on Aug 14,2019 4.1K Views

How To Implement Design Patterns in PHP?

edureka.co

In order to structure the code and project for your web application, there are a number of ways and you can put as much or as little thought as you like into architecting. But it is usually a good idea to follow common patterns as it helps us to make our code easier for others to understand and easier to manage. In this article we will explore Design Patterns In PHP.

Following pointers will be covered in this article,

Moving on with this article on Design Patterns in PHP

Design Patterns

General reusable solution for the common problems occurs in software design which is provided by design patterns in PHP. Relationships and interactions between classes or objects are shown by the patterns. This concept is used to speed up the development process by providing well tested, proven development/design paradigm. They are programming language independent strategies for solving a common problem which represents an idea, not a particular implementation. You can make your code more flexible, reusable and maintainable by using the design patterns . There are three types of design patterns. i.e Creational,structural, behavioral.

Creational Patterns: They are used to construct objects such that they can be decoupled from their implementing system

Structural Patterns: They are used to form large object structures between many disparate objects

Behavioral Patterns: They are used to manage algorithms, relationships, and responsibilities between objects

Moving on with this article on Design Patterns in PHP

Examples of Design Patterns

Moving on with this article on Design Patterns in PHP

Factory

It is a creational design pattern that solves problem of creating product objects without specifying their concrete classes. It is one of the most commonly used design patterns. We separate the making of objects into a dedicated class whose main responsibility is the making of objects, when we use the factory pattern. Let us consider the following example of the factory pattern:

<?php class Product { private $companyType; private $companyName; public function __construct($productBased, $Amazon) { $this->companyType = $productBased;
$this->companyName = $Amazon;
}
public function DesignModel()
{
return $this->companyType . ' ' . $this->companyName;
}
}
class DevelopProduct
{
public static function create($productBased, $Amazon)
{
return new Product($productBased, $Amazon);
}
}
$obj = DevelopProduct::create('automation', 'cloud service');
print_r($obj->DesignModel());
?>

The above code uses a factory to create the Product object. Benefits for building this code are:

Moving on with this article on Design Patterns in PHP

Singleton

In order to restrict the instantiation of a class to a single object, singleton pattern is used, which can be useful when only one object is required across the system. It often makes sense conceptually and architecturally to allow access to one and only one instance of a particular class while designing web applications. In order to prevent the direct creation of objects from the class, private constructor is used.

The only way to create an instance from the class is by using a static method that creates the object only if it wasn’t already created.The class has to provide global point of access to the unique instance. We end up with all the variables pointing to the same, single object as we restrict the number of objects that can be created from a class to only one. Following code demonstrates the concept of singleton concept. It is implemented based on static method creation is getInstance().

<?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()); $obj2 = SingletonChild::getInstance(); var_dump($obj2 === Singleton::getInstance()); var_dump($obj2 === SingletonChild::getInstance()); ?>

With this we come to an end of this article on Design Patterns In PHP. If you found this split in PHP blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”split in PHP” and I will get back to you.

BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework