PHP Design Patterns Prototype
About the Prototype
In the Prototype Pattern we create one standard object for each class, and clone that object to create new instances.
In this example we have an abstract BookPrototype class, with two specific or concrete subclasses, PHPBookPrototype and SQLBookPrototype. To create a object using either PHPBookPrototype or SQLBookPrototype we call the clone method.
BookPrototype.php
//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
abstract class BookPrototype {
protected $title;
protected $topic;
abstract function __clone();
function getTitle() {return $this->title;}
function setTitle($titleIn) {$this->title = $titleIn;}
function getTopic() {return $this->topic;}
}
PHPBookPrototype.php
//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
include_once('BookPrototype.php');
class PHPBookPrototype extends BookPrototype {
function __construct() {
$this->topic = 'PHP';
}
function __clone() {
}
}
SQLBookPrototype.php
//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
include_once('BookPrototype.php');
class SQLBookPrototype extends BookPrototype {
function __construct() {
$this->topic = 'SQL';
}
function __clone() {
}
}
testPrototype.php
//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
include_once('PHPBookPrototype.php');
include_once('SQLBookPrototype.php');
define('BR', '<'.'BR'.'>');
echo 'BEGIN TESTING PROTOTYPE PATTERN'.BR;
echo BR;
$phpProto = new PHPBookPrototype();
$sqlProto = new SQLBookPrototype();
$book1 = clone $sqlProto;
$book1->setTitle('SQL For Cats');
echo 'Book 1 topic: '.$book1->getTopic().BR;
echo 'Book 1 title: '.$book1->getTitle().BR;
echo BR;
$book2 = clone $phpProto;
$book2->setTitle('OReilly Learning PHP 5');
echo 'Book 2 topic: '.$book2->getTopic().BR;
echo 'Book 2 title: '.$book2->getTitle().BR;
echo BR;
$book3 = clone $sqlProto;
$book3->setTitle('OReilly Learning SQL');
echo 'Book 3 topic: '.$book3->getTopic().BR;
echo 'Book 3 title: '.$book3->getTitle().BR;
echo BR.BR;
echo 'END TESTING PROTOTYPE PATTERN'.BR;
output of testPrototype.php
BEGIN TESTING PROTOTYPE PATTERN
Book 1 topic: SQL Book 1 title: SQL For Cats
Book 2 topic: PHP Book 2 title: OReilly Learning PHP 5
Book 3 topic: SQL Book 3 title: OReilly Learning SQL
END TESTING PROTOTYPE PATTERN
References
| Comments Comments are left by visitors to FluffyCat.com, are not endorsed by FluffyCat.com, and may or may not be accurate. |
| Comment by baotou_lu Rate this Comment |
__clone, magic method, should be invoked after you use clone keyword to copy a object. it can do something to make sure the referenced object is copied if there is any class memeber is assigned by reference |
| Comment by cardmagic Rate this Comment |
In full prototype programming 1) you can modify the attributes of all objects directly, including the PHPBookPrototype object 2) cloning works on all objects, so you should be able to clone PHPBookPrototype (not instantiate it with a new operator) as well as cloning cloned objects like clone $book2 |
| Comment by Larry Rate this Comment |
Good question on having the abstract __clone() function defined but not really used. It probably would make a more interesting example if one of the concrete clone methods did something. |
| Comment by sumwatt Rate this Comment |
Out of curiosity, what is the purpose of declaring the abstract function __clone() in the parent object and then defining it in the children? Is it only to allow you some finite control over what happens when a specific object gets cloned? What would be the difference in just saying $copy = clone $original? |
| Sign In |
| to add your own comment |