PHP design patterns allow programmers and developers to develop robust software faster and also provides a way of encapsulating large ideas in friendly terms.
1. Abstract factory
Abstract Factory provides an interface to access various objects and ensure interaction of these objects, which realize the general behavior. It is used to provide access to databases, creating GUI, creating different connections, etc.
The example
<?php define('CONFIG_DB_TYPE', 'Mysql');
interface DBFactory { //Factories interface
public function getDB();
}
class MysqlFactory implements DBFactory {
public function getDB() {
return(new MysqlDB());
}}
class PgFactory implements DBFactory {
public function getDB() {
return(new PgDB());
}}
abstract class DB { // The abstract class, which realize the connection to database
private $_db;
public abstract function connect();
function __construct() {
$this->_path = realpath('.');
}
public function getConnection(){
return $this->_db;
}
public function setConnection($bd){
$this->_db = $bd;
}}
class MysqlDB extends DB {
public function connect() {
$db = mysql_connect();
$this->setConnection($db);
}}
class PgDB extends DB {
public function connect() {
$db = pg_connect('');
$this->setConnection($bd);; } }
class Application { //The class encapsulates logic of the creation of any of structures
public function __construct(DBFactory $factory) {
$db = $factory->getDB();
$db->connect();
$connection = $db->getConnection();
}}
class ApplicationRunner { //Using configuration, class creates the class to client to work with database.
public static function run() {
new Application(self::createDbSpecificFactory());
}
public static function createDbSpecificFactory() {
$className = CONFIG_DB_TYPE . 'Factory';
return new $className();
}}
ApplicationRunner::run();
?>
2. Singleton
Singleton ensures creating no more than one class example during the script firing. It is used to gain access to data, requiring only one-time processing, but does not change during script firing. For example, receiving a database connection or processing configurations data.
Example
<?php class Singleton {
protected static $instance;
private function __construct(){ } // Protection from creation through new Singleton
private function __clone() { } // Protection from creation through cloning
private function __wakeup() { } // Protection from creation through unserialize
public static function getInstance() { // Returns an instance of class Singleton
if ( empty(self::$instance) ) {
self::$instance = new Singleton ();
}
return self::$instance;
}
}
Singleton::getInstance();
?>
3. Decorator (Wrapper)
Decorator is designed to dynamically connect additional behavior to the designated object. This template represents a flexible alternative of creating subclasses practice to expand the functionality. It is used to expand the functionality of class methods, while not using imitation.
Example
<?
abstract class AbstractComponent {
abstract public function operation();
}
class ConcreteComponent extends AbstractComponent {
public function operation() { /*Some actions*/ }
}
abstract class AbstractDecorator extends AbstractComponent {
private $_component;
public function __construct(AbstractComponent $component){
$this->_component = $component;
}
public function operation() {
$this->_component->operation();
}
}
class ConcreteDecorator extends AbstractDecorator {
public function operation() {
// expanding functionality
parent::operation();
// expanding functionality
}
}
$decoratedComponent = new ConcreteDecorator(
new ConcreteComponent()
);
$decoratedComponent->operation();
?>