PageRenderTime 76ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/slow/array_access/436.php

http://github.com/facebook/hiphop-php
PHP | 101 lines | 100 code | 1 blank | 0 comment | 20 complexity | 3dade00dcc7fb11d57132d15d284adb7 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. function offsetGet($index) {
  3. echo ("GET0: $index\n");
  4. }
  5. class ArrayAccessImpl implements ArrayAccess {
  6. public $data = array();
  7. public function offsetUnset($index) {
  8. echo "UNSET: $index\n";
  9. }
  10. public function offsetSet($index, $value) {
  11. echo ("SET: $index\n");
  12. if(isset($data[$index])) {
  13. unset($data[$index]);
  14. }
  15. $u = &$this->data[$index];
  16. if(is_array($value)) {
  17. $u = new ArrayAccessImpl();
  18. foreach($value as $idx=>$e) $u[$idx]=$e;
  19. }
  20. else $u=$value;
  21. }
  22. public function offsetGet($index) {
  23. echo ("GET: $index\n");
  24. if(!isset($this->data[$index])) $this->data[$index]=new ArrayAccessImpl();
  25. return $this->data[$index];
  26. }
  27. public function offsetExists($index) {
  28. echo ("EXISTS: $index\n");
  29. if(isset($this->data[$index])) {
  30. if($this->data[$index] instanceof ArrayAccessImpl) {
  31. if(count($this->data[$index]->data)>0) return true;
  32. else return false;
  33. }
  34. else return true;
  35. }
  36. else return false;
  37. }
  38. }
  39. class ArrayAccessImpl2 extends ArrayAccessImpl {
  40. public function offsetUnset($index) {
  41. echo "UNSET2: $index\n";
  42. }
  43. public function offsetSet($index, $value) {
  44. echo ("SET2: $index\n");
  45. if(isset($data[$index])) {
  46. unset($data[$index]);
  47. }
  48. $u = &$this->data[$index];
  49. if(is_array($value)) {
  50. $u = new ArrayAccessImpl();
  51. foreach($value as $idx=>$e) $u[$idx]=$e;
  52. }
  53. else $u=$value;
  54. }
  55. public function offsetGet($index) {
  56. echo ("GET2: $index\n");
  57. if(!isset($this->data[$index])) $this->data[$index]=new ArrayAccessImpl();
  58. return $this->data[$index];
  59. }
  60. public function offsetExists($index) {
  61. echo ("EXISTS2: $index\n");
  62. if(isset($this->data[$index])) {
  63. if($this->data[$index] instanceof ArrayAccessImpl) {
  64. if(count($this->data[$index]->data)>0) return true;
  65. else return false;
  66. }
  67. else return true;
  68. }
  69. else return false;
  70. }
  71. }
  72. offsetGet('foo');
  73. $data = new ArrayAccessImpl();
  74. $data['string']="Just a simple string";
  75. $data['number']=33;
  76. $data['array']['another_string']="Alpha";
  77. $data['array']['some_object']=new stdClass();
  78. $data['array']['another_array']['x']['y']="Beta";
  79. $data['blank_array']=array();
  80. print_r(isset($data['array']));
  81. print_r($data['array']['non_existent']);
  82. print_r(isset($data['array']['non_existent']));
  83. print_r($data['blank_array']);
  84. print_r(isset($data['blank_array']));
  85. unset($data['blank_array']);
  86. print_r($data);
  87. $data2 = new ArrayAccessImpl2();
  88. $data2['string']="Just a simple string";
  89. $data2['number']=33;
  90. $data2['array']['another_string']="Alpha";
  91. $data2['array']['some_object']=new stdClass();
  92. $data2['array']['another_array']['x']['y']="Beta";
  93. $data2['blank_array']=array();
  94. print_r(isset($data2['array']));
  95. print_r($data2['array']['non_existent']);
  96. print_r(isset($data2['array']['non_existent']));
  97. print_r($data2['blank_array']);
  98. print_r(isset($data2['blank_array']));
  99. unset($data2['blank_array']);
  100. print_r($data2);