fork(6) download
  1. <?php
  2.  
  3.  
  4. class StringList implements ArrayAccess, IteratorAggregate
  5. {
  6. protected $items = [];
  7.  
  8. public function offsetSet($key, $value)
  9. {
  10. if (is_string($value)) {
  11.  
  12. $key ? $this->items[$key] = $value : array_push($this->items, $value);
  13.  
  14. return $this;
  15. }
  16.  
  17. throw new \UnexpectedValueException('Essa é uma lista que aceita somente string');
  18. }
  19.  
  20. public function offsetGet($key)
  21. {
  22. return $this->items[$key];
  23. }
  24.  
  25. public function offsetExists($key)
  26. {
  27. return isset($this->items[$key]);
  28. }
  29.  
  30. public function offsetUnset($key)
  31. {
  32. unset($this->items[$key]);
  33. }
  34.  
  35. public function getIterator()
  36. {
  37. return new ArrayIterator($this->items);
  38. }
  39. }
  40.  
  41.  
  42. $list = new StringList;
  43.  
  44. $list[] = 'Wallace';
  45. $list[] = 'Bigown';
  46. $list[] = 'Denner Carvalho';
  47.  
  48. foreach ($list as $string) {
  49. var_dump($string);
  50. }
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
string(7) "Wallace"
string(6) "Bigown"
string(15) "Denner Carvalho"