竹磬网-邵珠庆の日记 生命只有一次,你可以用它来做些更多伟大的事情–Make the world a little better and easier


271月/130

《Head First 设计模式》代码之PHP版

发布在 邵珠庆

《Head First 设计模式》是本不错的讲解设计模式的书,不像F4写的那么枯燥,应该算是比较容易理解的好书。书中的例子都比较浅显易懂,不过由于是外国佬写的,所以例子的习惯不是很附合中国特色,可能偶尔看起来有些别扭,还有语言习惯也不是中国风。当然看过这本书之后,你才能深刻理解设计模式到底能为你解决哪些题,不能为你解决哪些问题(比如不能代替你的编码)。

策略模式

 

[php] view plaincopy

 
  1. <?php  
  2. /** 
  3.  * 策略模式 
  4.  * 定义了算法族,分别封装起来,让它们之间可以互相替换, 
  5.  * 此模式让算法的变化独立于使用算法的客户。 
  6.  */  
  7. //飞行行为接口  
  8. interface FlyBehavior {  
  9.     public function fly();  
  10. }  
  11. //呱呱叫行为接口  
  12. interface QuackBehavior {  
  13.     public function quack();  
  14. }  
  15. //翅膀飞行  
  16. class FlyWithWings implements FlyBehavior {  
  17.     public function fly() {  
  18.         echo "I'm flying!!/n";  
  19.     }  
  20. }  
  21. //不会飞  
  22. class FlyNoWay implements FlyBehavior {  
  23.     public function fly() {  
  24.         echo "I can't fly!/n";  
  25.     }  
  26. }  
  27. class FlyRocketPowered implements FlyBehavior {  
  28.     public function fly() {  
  29.         echo "I'm flying with a rocket!/n";  
  30.     }  
  31. }  
  32. class Qquack implements QuackBehavior {  
  33.     public function quack() {  
  34.         echo "Quack/n";  
  35.     }  
  36. }  
  37. class Squeak implements QuackBehavior {  
  38.     public function quack() {  
  39.         echo "Squeak/n";  
  40.     }  
  41. }  
  42. class MuteQuack implements QuackBehavior {  
  43.     public function quack() {  
  44.         echo "<< Silence >>/n";  
  45.     }  
  46. }  
  47. abstract class Duck {  
  48.     protected $quack_obj;  
  49.     protected $fly_obj;  
  50.     public abstract function display();  
  51.     public function performQuack() {  
  52.         $this->quack_obj->quack();  
  53.     }  
  54.     public function performFly() {  
  55.         $this->fly_obj->fly();  
  56.     }  
  57.     public function swim() {  
  58.         echo "All ducks float, even decoys!/n";  
  59.     }  
  60.     public function setFlyBehavior(FlyBehavior $fb) {  
  61.         $this->fly_obj = $fb;  
  62.     }  
  63.     public function setQuackBehavior(QuackBehavior $qb) {  
  64.         $this->quack_obj = $qb;  
  65.     }  
  66. }  
  67. class ModelDuck extends Duck {  
  68.     public function __construct() {  
  69.         $this->fly_obj = new FlyNoWay();  
  70.         $this->quack_obj = new MuteQuack();  
  71.     }  
  72.     public function display() {  
  73.         echo "I'm a model duck!/n";  
  74.     }  
  75. }  
  76. $model = new ModelDuck();  
  77. $model->performFly();  
  78. $model->performQuack();  
  79. //提供新的能力  
  80. $model->setFlyBehavior(new FlyRocketPowered());  
  81. $model->setQuackBehavior(new Squeak());  
  82. $model->performFly();  
  83. $model->performQuack();  
  84. ?>  

 

单件模式

 

[php] view plaincopy

 
  1. <?php  
  2. /** 
  3.  * 单件模式 
  4.  * 确保一个类只有一个实例,并提供一个全局访问点。 
  5.  */  
  6. class MyClass {  
  7.     private static $uniqueInstance;  
  8.     private function __construct() {  
  9.     }  
  10.     public static function getInstance() {  
  11.         if (self::$uniqueInstance == null) {  
  12.             self::$uniqueInstance = new MyClass();  
  13.         }  
  14.         return self::$uniqueInstance;  
  15.     }  
  16. }  
  17. $myClass = MyClass::getInstance();  
  18. var_dump($myClass);  
  19. $myClass = MyClass::getInstance();  
  20. var_dump($myClass);  
  21. ?>  

 

工厂方法模式

 

[php] view plaincopy

 
  1. <?php  
  2. abstract class PizzaStore {  
  3.     public function orderPizza($type) {  
  4.         $pizza = $this->createPizza($type);  
  5.         $pizza->prepare();  
  6.         $pizza->bake();  
  7.         $pizza->cut();  
  8.         $pizza->box();  
  9.         return $pizza;  
  10.     }  
  11.     public abstract function createPizza($type);  
  12. }  
  13. class NYPizzaStore extends PizzaStore {  
  14.     public function createPizza($type) {  
  15.         if ($type == "cheese") {  
  16.             return new NYStyleCheesePizza();  
  17.         } elseif ($type == "veggie") {  
  18.             return new NYStyleVeggiePizza();  
  19.         } elseif ($type == "clam") {  
  20.             return new NYStyleClamPizza();  
  21.         } elseif ($type == "papperoni") {  
  22.             return new NYStylePapperoniPizza();  
  23.         } else {  
  24.             return null;  
  25.         }  
  26.     }  
  27. }  
  28. class ChicagoPizzaStore extends PizzaStore {  
  29.     public function createPizza($type) {  
  30.         if ($type == "cheese") {  
  31.             return new ChicagoStyleCheesePizza();  
  32.         } elseif ($type == "veggie") {  
  33.             return new ChicagoStyleVeggiePizza();  
  34.         } elseif ($type == "clam") {  
  35.             return new ChicagoStyleClamPizza();  
  36.         } elseif ($type == "papperoni") {  
  37.             return new ChicagoStylePapperoniPizza();  
  38.         } else {  
  39.             return null;  
  40.         }  
  41.     }  
  42. }  
  43. abstract class Pizza {  
  44.     public $name;  
  45.     public $dough;  
  46.     public $sauce;  
  47.     public $toppings = array();  
  48.     public function prepare() {  
  49.         echo "Preparing " . $this->name . "/n";  
  50.         echo "Yossing dough.../n";  
  51.         echo "Adding sauce.../n";  
  52.         echo "Adding toppings: /n";  
  53.         for ($i = 0; $i < count($this->toppings); $i++) {  
  54.             echo "    " . $this->toppings[$i] . "/n";  
  55.         }  
  56.     }  
  57.     public function bake() {  
  58.         echo "Bake for 25 minutes at 350/n";  
  59.     }  
  60.     public function cut() {  
  61.         echo "Cutting the pizza into diagonal slices/n";  
  62.     }  
  63.     public function box() {  
  64.         echo "Place pizza in official PizzaStore box/n";  
  65.     }  
  66.     public function getName() {  
  67.         return $this->name;  
  68.     }  
  69. }  
  70. class NYStyleCheesePizza extends Pizza {  
  71.     public function __construct() {  
  72.         $this->name = "NY Style Sauce and cheese Pizza";  
  73.         $this->dough = "Thin Crust Dough";  
  74.         $this->sauce = "Marinara Sauce";  
  75.         $this->toppings[] = "Grated Reggiano Cheese";  
  76.     }  
  77. }  
  78. class NYStyleVeggiePizza extends Pizza {  
  79.     public function __construct() {  
  80.         $this->name = "NY Style Sauce and veggie Pizza";  
  81.         $this->dough = "Thin Crust Dough";  
  82.         $this->sauce = "Marinara Sauce";  
  83.         $this->toppings[] = "Grated Reggiano veggie";  
  84.     }  
  85. }  
  86. class NYStyleClamPizza extends Pizza {  
  87.     public function __construct() {  
  88.         $this->name = "NY Style Sauce and clam Pizza";  
  89.         $this->dough = "Thin Crust Dough";  
  90.         $this->sauce = "Marinara Sauce";  
  91.         $this->toppings[] = "Grated Reggiano clam";  
  92.     }  
  93. }  
  94. class NYStylePapperoniPizza extends Pizza {  
  95.     public function __construct() {  
  96.         $this->name = "NY Style Sauce and papperoni Pizza";  
  97.         $this->dough = "Thin Crust Dough";  
  98.         $this->sauce = "Marinara Sauce";  
  99.         $this->toppings[] = "Grated Reggiano papperoni";  
  100.     }  
  101. }  
  102. class ChicagoStyleCheesePizza extends Pizza {  
  103.     public function __construct() {  
  104.         $this->name = "Chicago Style Deep Dish Cheese Pizza";  
  105.         $this->dough = "Extra Thick Crust Dough";  
  106.         $this->sauce = "Plum Tomato Sauce";  
  107.         $this->toppings[] = "Shredded Mozzarella Cheese";  
  108.     }  
  109.     public function cut() {  
  110.         echo "Cutting the pizza into square slices/n";  
  111.     }  
  112. }  
  113. $myStore = new NYPizzaStore();  
  114. $chicagoStore = new ChicagoPizzaStore();  
  115. $pizza = $myStore->orderPizza("cheese");  
  116. echo "Ethan ordered a " . $pizza->getName() . "/n";  
  117. $pizza = $chicagoStore->orderPizza("cheese");  
  118. echo "Ethan ordered a " . $pizza->getName() . "/n";  
  119. ?>  

 

工厂模式

 

[php] view plaincopy

 
  1. <?php  
  2. abstract class PizzaStore {  
  3.     public function orderPizza($type) {  
  4.         $pizza = $this->createPizza($type);  
  5.         $pizza->prepare();  
  6.         $pizza->bake();  
  7.         $pizza->cut();  
  8.         $pizza->box();  
  9.         return $pizza;  
  10.     }  
  11.     public abstract function createPizza($type);  
  12. }  
  13. class NYPizzaStore extends PizzaStore {  
  14.     public function createPizza($type) {  
  15.         $pizza = null;  
  16.         $ingredientFactory = new NYPizzaIngredientFactory();  
  17.         if ($type == "cheese") {  
  18.             $pizza = new CheesePizza($ingredientFactory);  
  19.             $pizza->setName('New York Style Cheese Pizza');  
  20.         } elseif ($type == "veggie") {  
  21.             $pizza = new VeggiePizza($ingredientFactory);  
  22.             $pizza->setName('New York Style Veggie Pizza');  
  23.         } elseif ($type == "clam") {  
  24.             $pizza = new ClamPizza($ingredientFactory);  
  25.             $pizza->setName('New York Style Clam Pizza');  
  26.         } elseif ($type == "papperoni") {  
  27.             $pizza = new PapperoniPizza($ingredientFactory);  
  28.             $pizza->setName('New York Style Papperoni Pizza');  
  29.         }  
  30.         return $pizza;  
  31.     }  
  32. }  
  33. class ChicagoPizzaStore extends PizzaStore {  
  34.     public function createPizza($type) {  
  35.         if ($type == "cheese") {  
  36.             return new ChicagoStyleCheesePizza();  
  37.         } elseif ($type == "veggie") {  
  38.             return new ChicagoStyleVeggiePizza();  
  39.         } elseif ($type == "clam") {  
  40.             return new ChicagoStyleClamPizza();  
  41.         } elseif ($type == "papperoni") {  
  42.             return new ChicagoStylePapperoniPizza();  
  43.         } else {  
  44.             return null;  
  45.         }  
  46.     }  
  47. }  
  48. interface PizzaIngredientFactory {  
  49.     public function createDough();  
  50.     public function createSauce();  
  51.     public function createCheese();  
  52.     public function createVeggies();  
  53.     public function createPepperoni();  
  54.     public function createClam();  
  55. }  
  56. class NYPizzaIngredientFactory implements PizzaIngredientFactory {  
  57.     public function createDough() {  
  58.         return new ThinCrustDough();  
  59.     }  
  60.     public function createSauce() {  
  61.         return new MarinaraSauce();  
  62.     }  
  63.     public function createCheese() {  
  64.         return new ReggianoCheese();  
  65.     }  
  66.     public function createVeggies() {  
  67.         $veggies = array(  
  68.         new Garlic(),  
  69.         new Onion(),  
  70.         new Mushroom(),  
  71.         new RedPepper(),  
  72.         );  
  73.         return $veggies;  
  74.     }  
  75.     public function createPepperoni() {  
  76.         return new SlicedPepperoni();  
  77.     }  
  78.     public function createClam() {  
  79.         return new FreshClams();  
  80.     }  
  81. }  
  82. class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {  
  83.     public function createDough() {  
  84.         return new ThickCrustDough();  
  85.     }  
  86.     public function createSauce() {  
  87.         return new PlumTomatoSauce();  
  88.     }  
  89.     public function createCheese() {  
  90.         return new Mozzarella();  
  91.     }  
  92.     public function createVeggies() {  
  93.         $veggies = array(  
  94.         new BlackOlives(),  
  95.         new Spinach(),  
  96.         new EggPlant(),  
  97.         );  
  98.         return $veggies;  
  99.     }  
  100.     public function createPepperoni() {  
  101.         return new SlicedPepperoni();  
  102.     }  
  103.     public function createClam() {  
  104.         return new FrozenClams();  
  105.     }  
  106. }  
  107. abstract class Pizza {  
  108.     public $name;  
  109.     public $dough;  
  110.     public $sauce;  
  111.     public $veggies = array();  
  112.     public $cheese;  
  113.     public $pepperoni;  
  114.     public $clam;  
  115.     public abstract function prepare();  
  116.     public function bake() {  
  117.         echo "Bake for 25 minutes at 350/n";  
  118.     }  
  119.     public function cut() {  
  120.         echo "Cutting the pizza into diagonal slices/n";  
  121.     }  
  122.     public function box() {  
  123.         echo "Place pizza in official PizzaStore box/n";  
  124.     }  
  125.     public function getName() {  
  126.         return $this->name;  
  127.     }  
  128.     public function setName($name) {  
  129.         $this->name = $name;  
  130.     }  
  131.     public function __toString() {  
  132.     }  
  133. }  
  134. class CheesePizza extends Pizza {  
  135.     public $ingredientFactory;  
  136.     public function __construct(PizzaIngredientFactory $ingredientFactory) {  
  137.         $this->ingredientFactory = $ingredientFactory;  
  138.     }  
  139.     public function prepare() {  
  140.         echo "Preparing " . $this->name . "/n";  
  141.         $this->dough = $this->ingredientFactory->createDough();  
  142.         $this->sauce = $this->ingredientFactory->createSauce();  
  143.         $this->cheese = $this->ingredientFactory->createCheese();  
  144.     }  
  145. }  
  146. class ClamPizza extends Pizza {  
  147.     public $ingredientFactory;  
  148.     public function __construct(PizzaIngredientFactory $ingredientFactory) {  
  149.         $this->ingredientFactory = $ingredientFactory;  
  150.     }  
  151.     public function prepare() {  
  152.         echo "Preparing " . $this->name . "/n";  
  153.         $this->dough = $this->ingredientFactory->createDough();  
  154.         $this->sauce = $this->ingredientFactory->createSauce();  
  155.         $this->cheese = $this->ingredientFactory->createCheese();  
  156.         $clam = $this->ingredientFactory->createClam();  
  157.     }  
  158. }  
  159. $nyPizzaStore = new NYPizzaStore();  
  160. $nyPizzaStore->orderPizza('cheese');  
  161. ?>  

 

观察者模式

 

[php] view plaincopy

 
  1. <?php  
  2. /** 
  3.  * 观察者模式 
  4.  * 定义了对象之间的一对多依赖,当一个对象改变状态时, 
  5.  * 它的所有依赖者都会收到通知并自动更新。 
  6.  */  
  7. interface Subject {  
  8.     public function registerObserver(Observer $o);  
  9.     public function removeObserver(Observer $o);  
  10.     public function notifyObservers();  
  11. }  
  12. interface Observer {  
  13.     public function update($temperature, $humidity, $pressure);  
  14. }  
  15. interface DisplayElement {  
  16.     public function display();  
  17. }  
  18. class WeatherData implements Subject {  
  19.     private $observers = array();  
  20.     private $temperature;  
  21.     private $humidity;  
  22.     private $pressure;  
  23.     public function __construct() {  
  24.         $this->observers = array();  
  25.     }  
  26.     public function registerObserver(Observer $o) {  
  27.         $this->observers[] = $o;  
  28.     }  
  29.     public function removeObserver(Observer $o) {  
  30.         if (($key = array_search($o, $this->observers)) !== false) {  
  31.             unset($this->observers[$key]);  
  32.         }  
  33.     }  
  34.     public function notifyObservers() {  
  35.         foreach ($this->observers as $observer) {  
  36.             $observer->update($this->temperature, $this->humidity, $this->pressure);  
  37.         }  
  38.     }  
  39.     public function measurementsChanged() {  
  40.         $this->notifyObservers();  
  41.     }  
  42.     public function setMeasurements($temperature, $humidity, $pressure) {  
  43.         $this->temperature = $temperature;  
  44.         $this->humidity = $humidity;  
  45.         $this->pressure = $pressure;  
  46.         $this->measurementsChanged();  
  47.     }  
  48. }  
  49. class CurrentConditionsDisplay implements Observer, DisplayElement {  
  50.     private $temperature;  
  51.     private $humidity;  
  52.     private $weatherData;  
  53.     public function __construct(Subject $weatherData) {  
  54.         $this->weatherData = $weatherData;  
  55.         $weatherData->registerObserver($this);  
  56.     }  
  57.     public function update($temperature, $humidity, $pressure) {  
  58.         $this->temperature = $temperature;  
  59.         $this->humidity = $humidity;  
  60.         $this->display();  
  61.     }  
  62.     public function display() {  
  63.         echo "温度:" . $this->temperature . "; 湿度:" . $this->humidity . "%/n";  
  64.     }  
  65. }  
  66. class StatistionsDisplay implements Observer, DisplayElement {  
  67.     private $temperature;  
  68.     private $humidity;  
  69.     private $pressure;  
  70.     private $weatherData;  
  71.     public function __construct(Subject $weatherData) {  
  72.         $this->weatherData = $weatherData;  
  73.         $weatherData->registerObserver($this);  
  74.     }  
  75.     public function update($temperature, $humidity, $pressure) {  
  76.         $this->temperature = $temperature;  
  77.         $this->humidity = $humidity;  
  78.         $this->pressure = $pressure;  
  79.         $this->display();  
  80.     }  
  81.     public function display() {  
  82.         echo "温度:" . $this->temperature . "; 湿度:" . $this->humidity . "%; 气压:" . $this->pressure . "/n";  
  83.     }  
  84. }  
  85. $weatherData = new WeatherData();  
  86. $currentDisplay = new CurrentConditionsDisplay($weatherData);  
  87. $statistionDisplay = new StatistionsDisplay($weatherData);  
  88. $weatherData->setMeasurements(10, 20, 30);  
  89. $weatherData->removeObserver($currentDisplay);  
  90. $weatherData->setMeasurements(30, 40, 50);  
  91. ?>  

 

命令模式

 

[php] view plaincopy

 
  1. <?php  
  2. class Light {  
  3.     public function __construct() {  
  4.     }  
  5.     public function on() {  
  6.         echo "Light On/n";  
  7.     }  
  8.     public function off() {  
  9.         echo "Light Off/n";  
  10.     }  
  11. }  
  12. interface Command {  
  13.     public function execute();  
  14. }  
  15. class LightOnCommand implements Command {  
  16.     public $light;  
  17.     public function __construct(Light $light) {  
  18.         $this->light = $light;  
  19.     }  
  20.     public function execute() {  
  21.         $this->light->on();  
  22.     }  
  23. }  
  24. class SimpleRemoteControl {  
  25.     public $slot;  
  26.     public function __construct() {  
  27.     }  
  28.     public function setCommand(Command $command) {  
  29.         $this->slot = $command;  
  30.     }  
  31.     public function buttonWasPressed() {  
  32.         $this->slot->execute();  
  33.     }  
  34. }  
  35. class RemoteControlTest {  
  36.     public static function main() {  
  37.         $remote = new SimpleRemoteControl();  
  38.         $light = new Light();  
  39.         $lightOn = new LightOnCommand($light);  
  40.         $remote->setCommand($lightOn);  
  41.         $remote->buttonWasPressed();  
  42.     }  
  43. }  
  44. RemoteControlTest::main();  
  45. ?>  

 

 

装饰者模式

 

[php] view plaincopy

 
  1. <?php  
  2. /** 
  3.  * 装饰着模式 
  4.  * 动态地将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有弹性的替代方案。 
  5.  */  
  6. abstract class Beverage {  
  7.     public $description = "Unknown Beverage";  
  8.     public function getDescription() {  
  9.         return $this->description;  
  10.     }  
  11.     public abstract function cost();  
  12. }  
  13. abstract class CondimentDecorator extends Beverage {  
  14.     //JAVA代码里这里是个抽象类,PHP不允许这么做  
  15.     public function getDescription() {  
  16.         return $this->description;  
  17.     }  
  18. }  
  19. class Espresso extends Beverage {  
  20.     public function __construct() {  
  21.         $this->description = "Espresso";  
  22.     }  
  23.     public function cost() {  
  24.         return 1.99;  
  25.     }  
  26. }  
  27. class HouseBlend extends Beverage {  
  28.     public function __construct() {  
  29.         $this->description = "HouseBlend";  
  30.     }  
  31.     public function cost() {  
  32.         return .89;  
  33.     }  
  34. }  
  35. class DarkRoast extends Beverage {  
  36.     public function __construct() {  
  37.         $this->description = "DarkRoast";  
  38.     }  
  39.     public function cost() {  
  40.         return .99;  
  41.     }  
  42. }  
  43. class Mocha extends CondimentDecorator {  
  44.     public $beverage;  
  45.     public function __construct(Beverage $beverage) {  
  46.         $this->beverage = $beverage;  
  47.     }  
  48.     public function getDescription() {  
  49.         return $this->beverage->getDescription() . ", Mocha";  
  50.     }  
  51.     public function cost() {  
  52.         return .20 + $this->beverage->cost();  
  53.     }  
  54. }  
  55. class Whip extends CondimentDecorator {  
  56.     public $beverage;  
  57.     public function __construct(Beverage $beverage) {  
  58.         $this->beverage = $beverage;  
  59.     }  
  60.     public function getDescription() {  
  61.         return $this->beverage->getDescription() . ", Whip";  
  62.     }  
  63.     public function cost() {  
  64.         return .10 + $this->beverage->cost();  
  65.     }  
  66. }  
  67. class Soy extends CondimentDecorator {  
  68.     public $beverage;  
  69.     public function __construct(Beverage $beverage) {  
  70.         $this->beverage = $beverage;  
  71.     }  
  72.     public function getDescription() {  
  73.         return $this->beverage->getDescription() . ", Soy";  
  74.     }  
  75.     public function cost() {  
  76.         return .15 + $this->beverage->cost();  
  77.     }  
  78. }  
  79. $beverage = new Espresso();  
  80. echo $beverage->getDescription() . "/n";  
  81. $beverage2 = new DarkRoast();  
  82. $beverage2 = new Mocha($beverage2);  
  83. $beverage2 = new Mocha($beverage2);  
  84. $beverage2 = new Whip($beverage2);  
  85. echo $beverage2->getDescription() . " $" . $beverage2->cost() . "/n";  
  86. $beverage3 = new HouseBlend();  
  87. $beverage3 = new Soy($beverage3);  
  88. $beverage3 = new Mocha($beverage3);  
  89. $beverage3 = new Whip($beverage3);  
  90. echo $beverage3->getDescription() . " $" . $beverage3->cost() . "/n";  
  91. ?>  

 

状态模式

 

[php] view plaincopy

 
  1. <?php  
  2. class GumballMachine {  
  3.     const SOLD_OUT = 0;  
  4.     const NO_QUARTER = 1;  
  5.     const HAS_QUARTER = 2;  
  6.     const SOLD = 3;  
  7.     public $state = self::SOLD_OUT;  
  8.     public $count = 0;  
  9.     public function __construct($count) {  
  10.         $this->count = $count;  
  11.         if ($count > 0) {  
  12.             $this->state = self::NO_QUARTER;  
  13.         }  
  14.     }  
  15.     public function insertQuarter() {  
  16.         if ($this->state == self::HAS_QUARTER) {  
  17.             echo "You can't insert another quarter!/n";  
  18.         } else if ($this->state == self::NO_QUARTER) {  
  19.             $this->state = self::HAS_QUARTER;  
  20.             echo "You inserted a quarter!/n";  
  21.         } else if ($this->state == self::SOLD_OUT) {  
  22.             echo "You can't insert a quarter, the machine is sold out!/n";  
  23.         } else if ($this->state == self::SOLD) {  
  24.             echo "Please wait, we're already giving you a gumball!/n";  
  25.         }  
  26.     }  
  27. }  
  28. $obj = new GumballMachine(0);  
  29. print_r($obj)  
  30. ?>