Datasheet
20
x
CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK
<?php
abstract class CarPrototype {
protected $model;
protected $color;
abstract function __clone();
function getModel() {
return $this->model;
}
function getColor() {
return $this->color;
}
function setColor($colorIn) {
$this->color= $colorIn;
}
}
class DodgeCarPrototype extends CarPrototype {
function __construct() {
$this->model = ‘Dodge Magnum’;
}
function __clone() {
}
}
class SubaruCarPrototype extends CarPrototype {
function __construct() {
$this->model = ‘Subaru Outback’;
}
function __clone() {
}
}
?>
code snippet /prototype/CarPrototype.class.php
Cars are quite an accurate example here, because in real life a prototype is created by a manufac-
turer and then different models are based on this prototype and fi lled with unique features. The
following code tests the preceding classes. First, it creates two Prototype objects as showcase cars
and then clones one of them to serve the customer. Then the color can be picked by the uniform
interface.
<?php
include_once(‘CarPrototype.class.php’);
$dodgeProto= new DodgeCarPrototype();
$subaruProto = new SubaruCarPrototype();
echo ‘Which car do you want? <br />’;
$customerDecision = ‘Subaru’;
if( $customerDecision == ‘Subaru’ ){
$customersCar = clone $subaruProto;
} else {
$customersCar = clone $dodgeProto;
}
echo $customersCar->getModel().’<br />’;
echo ‘What color do you want?<br />’;
$customersCar->setColor(‘red’);
c01.indd 20c01.indd 20 1/24/2011 5:45:19 PM1/24/2011 5:45:19 PM