PHP Design patterns : Part 2 – Template pattern
The Template pattern is the use of a class that inherits a class and adds functionality that can prevent redundancy in code usage.
To use the adapter class from part 1, you need to constantly specify the connection parameters to the database. To prevent specifying the database details in every php file, we can use the template pattern and extend the adapter class to create a subclass that defines the connection parameters as constants.
1 2 3 4 5 6 7 8 9 | class DB_Mysql_Test extends DB_Mysql { protected $user = “w3user”; protected $pass = “w3pass”; protected $dbhost = “localhost”; protected $dbname = “articles”; public function __construct() { //implementation using $this->user,$this->pass,$this->dbhost,$this->dbname } // more implementation of class. } |
Using the above template patter wrapper classes:
1 2 3 | $dbh = new DB_Mysql_Test(); $statemt = $dbh->prepare(“SELECT * FROM articles WHERE author = :1”); $statemt->execute(); |
PHP Design patterns : Part 1 – Adapter pattern
The Adapter pattern is used to provide access to an object via a specific interface. An adapter allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface. The adapter translates calls to its interface, into calls to the original interface, and the amount of code necessary to do this is typically small. The adapter is also responsible for transforming data into appropriate forms. In PHP we most often see this pattern as providing an alternative interface to a set of procedural routines such as database connect, query and retrieve routines. Wrapper classes are generally an implementation of the adapter pattern.
Example wrapper class illustrating adapter pattern:
1 2 3 4 5 6 7 8 9 10 11 | class DB_Mysql { protected $user; protected $pass; protected $dbhost; protected $dbname; protected $dbh; public function connect() { //implementation } public function prepare() { //implementation } public function execute() { //implementation } } |
1 2 3 4 5 6 7 8 9 | class DB_MysqlStatement { protected $result; public $query; protected $dbh; public function execute() { //implementation } public function fetch_row() { //implementation } public function fetch_assoc() { //implementation } } |
Using the above wrapper classes:
1 2 3 | $dbh = new DB_Mysql(“w3user”, “w3pass”, “localhost”, “w3db”); $statemt = $dbh->prepare(“SELECT * FROM articles WHERE author = :1”); $statemt->execute(); |
PHP Design patterns : Part 1 – Introduction
Design patterns are generalized solutions to classes of problems that software developers encounter frequently. The fundamental idea of design patterns is that problems and their corresponding solutions tend to follow repeatable patterns. A design pattern is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.
