Advertisement
dragonbe

Fluent interfaces

Nov 14th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.61 KB | None | 0 0
  1. <?php
  2. class Cupboard
  3. {
  4.     protected $_items;
  5.    
  6.     public function addItem($item)
  7.     {
  8.         $this->_items[] = $item;
  9.         return $this;
  10.     }
  11.     public function removeItem($item)
  12.     {
  13.         $key = array_search($item, $this->_items);
  14.         unset ($this->_items[$key]);
  15.         return $this;
  16.     }
  17.     public function listItems()
  18.     {
  19.         return implode(', ', $this->_items);
  20.     }
  21.    
  22. }
  23.  
  24. $cb = new Cupboard();
  25. echo $cb->addItem('plate')
  26.         ->addItem('cup')
  27.         ->addItem('spoon')
  28.         ->addItem('fork')
  29.         ->removeItem('spoon')
  30.         ->listItems() . PHP_EOL;
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement