What is design pattern in php

TechAlpine – All About Technology

PHP design Patterns

Overview: We all know how important the design patterns have become an essential part in today’s application development, be it any technology. In the PHP technology also design pattern have become integral part of the development. Commonly used design patterns are also followed in the PHP development world.

In this article I will talk about the different design patterns which are used in the PHP domain with some sample applications.

Introduction: Design patterns provide a generic reusable solution to common problems. Design pattern is not a concrete solution which can be converted in to source code or a machine code rather it is a template which can be used to solve a problem in different situations. Design patterns help in faster development as the templates are proven and from the developer’s point, only implementation is required. Design patterns not only make software development faster but also encapsulate big ideas in a simpler way. In the PHP world, following five design patterns are used in general –

  • The Factory Pattern
  • The Singleton Pattern
  • The Observer Pattern
  • The Chain of Command Pattern
  • The Strategy Pattern

The Factory Pattern:

The factory pattern provides a solution which enables loose coupling, thus helps us to get rid of tight coupling problem. As a developer we all are well aware of the changes faced while we maintain a code, which is tightly coupled. Factory pattern provides a class having some methods to create objects for us at runtime. Instead of creating the instance directly we use the factory to create the objects for us. Thus, if we have to change the type of the object created, we just need to change the factory. The following code is an example of factory implementation in PHP –

Читайте также:  Удаление всех переменных php

Listing 1: A sample factory implementation in PHP

class Customer implements Customer

public function __construct ( $id )

return “PHP – Factory Pattern”;

Источник

Оцените статью