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(); |