When deciding your software architecture it makes sense to design your classes as generic as possible. In terms of PHP coding one of the best ways to do this is to use the 'Strategy pattern'. The strategy pattern aims to put the non-generic (or less specific) functionality into a 'default' class otherwise known as a Base or Abstract Class. The pattern then works by creating more specific (less generic) classes to represent each class or object within your application.
One of the main requirement of this Pattern is to build up an Interface, or a 'set of methods' that all relatives of this Base Class would have access to, or would be required to overload or implement. The main benefit of this pattern is that it allows you treat all the classes as the same type, knowing the interface up front, or the set of methods that will be adhered to.
This way you can code against the Interface or Type, and be sure that created object will be of the correct type (see Factory Pattern) and will call the correct behaviour based on the implementation of the (derived class). You can also view the UML diagram for this pattern below.
.
Rather than create a large application that uses this and other Patterns across multiple modules, we have decided to just implement a basic example with very little boiler code, just to keep the example easy to understand and grasp.
The example below uses a factious website that requires content, from different sources, depending on the 'page type'. For instance, the page content would actually be coming from: Text Files, XML Files, Static DB, DB Dataset, RSS, Etc. The point of using the Strategy Pattern is to build this behaviour into a set of classes which encapsulate the behaviour, behind an interface, into the correct implementation (of the class, and created objects).
Notice how the example below calls the correct readPage() and writePage() methods without know the exact type or class being accessed! By designing your software architecture generically, it allows you design at a high level looking at the state and relationship between generic classes, rather than ALL the specific classes and objects.
<?php
abstract class PageContent{
//both abstract, need to be implemented in derived classes
abstract function readPage();
abstract function writePage();
}
class StaticDB extends PageContent{
private $dbcontent=array();
function readPage(){
//get the content (static database)
$dcontent="CSS and XHTML content from your DB";
array_push($this->dbcontent, $dcontent);
}
function writePage(){
//do somthing with the content
print_r($this->dbcontent);
echo"<br>";
}
}
class StaticTxt extends PageContent{
private $txtcontent=array();
function readPage(){
//get the content (txt file)
$tcontent="CSS and XHTML content from your TXT file";
array_push($this->txtcontent, $tcontent);
}
function writePage(){
//return array content
print_r($this->txtcontent);
echo"<br>";
}
}
//end of class definitions - function below to test objects
function getpage(PageContent $obj){
if($obj){
$obj->readPage();
$obj->writePage();
}
}
$dbpage=new StaticDB();
$txtpage=new StaticTxt();
getpage($dbpage);
getpage($txtpage);
unset($dbpage,$txtpage);
?>