Advertisement
fruffl

Iterator::key() as object

Sep 21st, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.56 KB | None | 0 0
  1.  
  2.     class b implements Iterator
  3.     {
  4.         private $p  = 0;
  5.         private $d  = [];
  6.        
  7.         function add($o1, $o2)
  8.         {
  9.             $this->d[] = [$o1, $o2];
  10.         }
  11.        
  12.         function key()
  13.         {
  14.             return $this->d[$this->p][0];
  15.         }
  16.        
  17.         function rewind()
  18.         {
  19.             $this->p = 0;
  20.         }
  21.        
  22.         function current()
  23.         {
  24.             return $this->d[$this->p][1];
  25.         }
  26.        
  27.         function next()
  28.         {
  29.             ++$this->p;
  30.         }
  31.        
  32.        
  33.         function valid()
  34.         {
  35.             return isset($this->d[$this->p]);
  36.         }
  37.        
  38.     }
  39.    
  40.     $b = new b;
  41.     $b->add(new Str, new Str);
  42.    
  43.    
  44.     foreach($b as $k => $v)
  45.         var_dump($k); // object Str
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement