Skip to content

Instantly share code, notes, and snippets.

@phcostabh
Last active December 17, 2015 14:49
Show Gist options
  • Save phcostabh/5627673 to your computer and use it in GitHub Desktop.
Save phcostabh/5627673 to your computer and use it in GitHub Desktop.
<?php
interface Pizza
{
public function getDescription();
public function getPrice();
}
class PlainPizza implements Pizza
{
public function getDescription()
{
return 'Massa';
}
public function getPrice()
{
return 15.00;
}
}
class Topping implements Pizza
{
protected $_pizza;
public function __construct(Pizza $new_pizza)
{
$this->_pizza = $new_pizza;
}
public function getDescription()
{
return $this->_pizza->getDescription();
}
public function getPrice()
{
return $this->_pizza->getPrice();
}
}
class Mozarella extends Topping
{
public function getDescription(){
return $this->_pizza->getDescription() . ', Mozarella';
}
public function getPrice()
{
return $this->_pizza->getPrice() + 5.00;
}
}
class Ham extends Topping
{
public function getDescription(){
return $this->_pizza->getDescription() . ', Ham';
}
public function getPrice()
{
return $this->_pizza->getPrice() + 5.00;
}
}
$my_pizza = new Mozarella(new PlainPizza);
echo $my_pizza->getDescription(), PHP_EOL;
echo $my_pizza->getPrice(), PHP_EOL;
$ham = new Ham($my_pizza);
echo $ham->getDescription(), PHP_EOL;
echo $ham->getPrice(), PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment