PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/quick/array_access.php

http://github.com/facebook/hiphop-php
PHP | 159 lines | 131 code | 17 blank | 11 comment | 3 complexity | afb4dfb39edc621026406524162a6d10 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. print "Test begin\n";
  3. class E implements ArrayAccess {
  4. public function __construct() {
  5. $this->i = ++self::$count;
  6. }
  7. public function __destruct() {
  8. printf("In E::__destruct() %d\n", $this->i);;
  9. }
  10. public function offsetGet($offset) {
  11. print "In E::offsetGet()\n";
  12. $a = array();
  13. $a[] = "hello";
  14. $a[] = new E; // to make sure the array's life cycle is correct
  15. return $a;
  16. }
  17. public function offsetExists($offset) {}
  18. public function offsetSet($offset, $value) {}
  19. public function offsetUnset($offset) {}
  20. private static $count = 0;
  21. private $i;
  22. }
  23. class D implements ArrayAccess {
  24. public function __destruct() {
  25. print "In D::__destruct()\n";
  26. }
  27. public function offsetGet($offset) {
  28. print "In D::offsetGet()\n";
  29. # Generate a new object that has no references besides the one being
  30. # returned, and return it.
  31. return new E;
  32. }
  33. public function offsetExists($offset) {}
  34. public function offsetSet($offset, $value) {}
  35. public function offsetUnset($offset) {}
  36. }
  37. class C implements ArrayAccess {
  38. public function offsetGet($offset) {
  39. print "In C::offsetGet()\n";
  40. # Generate a new object that has no references besides the one being
  41. # returned, and return it.
  42. return new D;
  43. }
  44. public function offsetExists($offset) {}
  45. public function offsetSet($offset, $value) {}
  46. public function offsetUnset($offset) {}
  47. }
  48. function main1() {
  49. $c = new C;
  50. # Dereference the object returned by $c['x'], all in a single expression. The
  51. # VM must retain a reference to the intermediate result for long enough to
  52. # complete the next dereference operation.
  53. $x = $c['d']['e'][0][0];
  54. var_dump($x);
  55. $c['x']['y'][0][0] = "goodbye";
  56. }
  57. main1();
  58. class cls implements arrayaccess {
  59. private $container = array();
  60. public function __construct() {
  61. $this->container = array(
  62. "one" => 1,
  63. "two" => 2,
  64. "three" => 3,
  65. );
  66. }
  67. public function offsetGet($offset) {
  68. print "In cls::offsetGet($offset)\n";
  69. return isset($this->container[$offset]) ? $this->container[$offset] : null;
  70. }
  71. public function offsetExists($offset) {
  72. print "In cls::offsetExists($offset)\n";
  73. return isset($this->container[$offset]);
  74. }
  75. public function offsetSet($offset, $value) {
  76. print "In cls::offsetSet($offset, $value)\n";
  77. if (is_null($offset)) {
  78. $this->container[] = $value;
  79. } else {
  80. $this->container[$offset] = $value;
  81. }
  82. }
  83. public function offsetUnset($offset) {
  84. print "In cls::offsetUnset($offset)\n";
  85. unset($this->container[$offset]);
  86. }
  87. }
  88. function main2() {
  89. $obj = new cls;
  90. var_dump(isset($obj["two"]));
  91. var_dump($obj["two"]);
  92. unset($obj["two"]);
  93. $a = array($obj);
  94. $obj["two"] = 2;
  95. unset($a[0]["two"]);
  96. var_dump(isset($obj["two"]));
  97. $obj["two"] = "A value";
  98. var_dump($obj["two"]);
  99. $obj[] = 'Append 1';
  100. $obj[] = 'Append 2';
  101. $obj[] = 'Append 3';
  102. $obj[][0] = "lost"; # offsetGet() returns null by value, so array("lost") is
  103. # lost.
  104. # SetOpElem.
  105. $obj["x"] += 1;
  106. var_dump($obj["x"]);
  107. $obj["x"] -= 1;
  108. var_dump($obj["x"]);
  109. # IncDecElem.
  110. $obj["x"]++;
  111. var_dump($obj["x"]);
  112. $obj["x"]--;
  113. var_dump($obj["x"]);
  114. ++$obj["x"];
  115. var_dump($obj["x"]);
  116. --$obj["x"];
  117. var_dump($obj["x"]);
  118. $obj[] = "SetNewElem";
  119. $obj[] += 1; # SetOpNewElem.
  120. $obj[]++; # IncDecNewElem.
  121. # UnsetElem.
  122. print_r($obj);
  123. unset($obj["x"]);
  124. print_r($obj);
  125. print "Test end\n";
  126. }
  127. main2();
  128. class stringdoubler implements ArrayAccess {
  129. public function offsetExists($i) {
  130. return is_string($i);
  131. }
  132. public function offsetGet($i) {
  133. return $i . $i;
  134. }
  135. public function offsetSet($i, $v) {}
  136. public function offsetUnset($i) {}
  137. }
  138. function main3($a, $b, $c) {
  139. if (false) {}
  140. return $a[$b][$c];
  141. }
  142. var_dump(main3(array(new stringdoubler()), 0, 'hello'));