PHP Design Patterns .

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;}

}
To download source right-click here and "Save As...".


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() { }

}
To download source right-click here and "Save As...".

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() { }

}
To download source right-click here and "Save As...".


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;
To download source right-click here and "Save As...".

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


Design Patterns
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

PHP 5
The Official PHP web site
Core PHP Programming, 3rd Edition by Leon Atkinson and Zeev Suraski
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by baotou_lu on 2014-06-22 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 on 2008-09-15 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 on 2007-03-03 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 on 2007-02-02 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 comment on PHP Design Patterns Prototype.