fork(2) download
  1. <?php
  2.  
  3.  
  4. class DomainData implements ArrayAccess, Iterator
  5. {
  6. private $position = 0;
  7. public $domainId;
  8. public $color;
  9.  
  10. public function __construct($data = array())
  11. {
  12. $this->position = 0;
  13. foreach ($data as $key => $value) {
  14. $this[$key] = $value;
  15. }
  16. }
  17.  
  18. public function offsetExists($offset)
  19. {
  20. return isset($this->$offset);
  21. }
  22.  
  23. public function offsetSet($offset, $value)
  24. {
  25. $this->$offset = $value;
  26. }
  27.  
  28. public function offsetGet($offset)
  29. {
  30. return $this->$offset;
  31. }
  32.  
  33. public function offsetUnset($offset)
  34. {
  35. $this->$offset = null;
  36. }
  37.  
  38. /*****************************************************************/
  39. /* Iterator Implementation */
  40. /*****************************************************************/
  41.  
  42. public function rewind()
  43. {
  44. $this->position = 0;
  45. }
  46.  
  47. public function current()
  48. {
  49. return $this[$this->position];
  50. }
  51.  
  52. public function key()
  53. {
  54. return $this->position;
  55. }
  56.  
  57. public function next()
  58. {
  59. ++$this->position;
  60. }
  61.  
  62. public function valid()
  63. {
  64. return isset($this[$this->position]);
  65. }
  66. }
  67.  
  68.  
  69. $domainData = new DomainData([
  70. "domainId" => 1,
  71. "color" => "red"
  72. ]);
  73.  
  74. var_dump($domainData);
  75.  
  76. foreach($domainData as $k => $v){
  77. var_dump("domainData[$k] = $v");
  78. }
Success #stdin #stdout 0.02s 24668KB
stdin
Standard input is empty
stdout
object(DomainData)#1 (3) {
  ["position":"DomainData":private]=>
  int(0)
  ["domainId"]=>
  int(1)
  ["color"]=>
  string(3) "red"
}