probeer eens iets als dit te maken:
<?php
class factory {
public $name = null;
public $produced = array();
protected $products = array();
public function __construct($name) {
$this->name = $name;
}
public function get_name() {
return $this->name;
}
public function get_produced() {
return $this->produced;
}
public function add_product($product) {
$this->products[$product->get_name()] = $product;
return $this;
}
public function produce($name, $amount) {
if ( ! isset($this->products[$name]))
return false;
for ($i = 0; $i < $amount; ++$i) {
$this->produced[] = clone $this->products[$name];
}
return true;
}
}
class ice_factory extends factory {
protected $temperature = 5;
public function produce($name, $amount) {
if($this->temperature > 11) {
echo 'het ijs smelt!<br />'.PHP_EOL;
return false;
}
$this->temperatuur += $amount;
return parent::produce($name, $amount);
}
public function cool_down() {
$this->temperature -= 3;
return $this;
}
}
class ice {
public $temperature = 0;
protected $name;
protected $colors = array();
protected $tastes = array();
public function __construct($name, $colors, $tastes, $temperature = null) {
if($temperature > 11) {
echo'het ijs smelt!<br />'.PHP_EOL;
return false;
}
$this->name = $name;
$this->colors = is_array($colors) ? $colors : array($colors);
$this->tastes = is_array($tastes) ? $tastes : array($tastes);
if(is_int($temperature)) {
$this->temperature = $temperature;
}
}
public function get_name() {
if($temperature > 11) {
echo'het ijs smelt! je kunt niet meer herkennen welk ijs het is!<br />'.PHP_EOL;
return false;
}
return $this->name;
}
public function get_colors() {
if($temperature > 11) {
echo'het ijs smelt! je kunt niet meer herkennen welke kleuren het ijs had!<br />'.PHP_EOL;
return false;
}
return $this->colors;
}
public function get_tastes() {
if($temperature > 11) {
echo'het ijs smelt! proef snel! voordat je het weg moet gooien<br />'.PHP_EOL;
}
return $this->tastes;
}
public function get_temperature() {
return $this->temperature;
}
}
$ijs_fabriek = new ice_factory('olla');
$ijs_fabriek->add_product(new ice('geel soft ijs', 'geel', 'soft ijs'))
->add_product(new ice('rood soft ijs', 'rood', 'soft ijs'))
->add_product(new ice('paars soft ijs', 'paars', 'soft ijs'))
->add_product(new ice('orange soft ijs', 'orange', 'soft ijs'))
->add_product(new ice('raketje', array('rood', 'geel', 'orange'), 'water'));
$ijs_fabriek->produce('geel soft ijs', 3);
$ijs_fabriek->produce('paars soft ijs', 8);
$ijs_fabriek->cool_down()->cool_down()->produce('raketje', 30);
echo $ijs_fabriek->get_name().' heeft de volgende ijsjes geproduceerd:<br />'.PHP_EOL;
echo '<pre>'.print_r($ijs_fabriek->get_produced(), true).'</pre><br />'.PHP_EOL;
echo 'de temperatuur is opgelopen tot '.$ijsfabriek->temperature.' graden celcius.';
?>
<hr />
<?php
highlight_file(__FILE__);
?>
Toon Meer
geen idee of het fouten bevat.
maar bekijk de code, en hopelijk begrijp je het.
elk ijsje wordt vertegenwoordigd door een object.
en de fabriek is ook weer een object...