bug66834.phpt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. --TEST--
  2. SPL: Bug #66834
  3. --FILE--
  4. <?php
  5. // overrides both offsetExists and offsetGet
  6. class ArrayObjectBoth extends ArrayObject
  7. {
  8. public function offsetExists($offset): bool {
  9. var_dump('Called: '.__METHOD__);
  10. return parent::offsetExists($offset);
  11. }
  12. public function offsetGet($offset): mixed {
  13. var_dump('Called: '.__METHOD__);
  14. return parent::offsetGet($offset);
  15. }
  16. }
  17. // overrides only offsetExists
  18. class ArrayObjectExists extends ArrayObject
  19. {
  20. public function offsetExists($offset): bool {
  21. var_dump('Called: '.__METHOD__);
  22. return parent::offsetExists($offset);
  23. }
  24. }
  25. // overrides only offsetGet
  26. class ArrayObjectGet extends ArrayObject
  27. {
  28. public function offsetGet($offset): mixed {
  29. var_dump('Called: '.__METHOD__);
  30. return parent::offsetGet($offset);
  31. }
  32. }
  33. // overrides only offsetGet and offsetSet
  34. class ArrayObjectGetSet extends ArrayObject
  35. {
  36. public function offsetGet($offset): mixed
  37. {
  38. return parent::offsetGet(str_rot13($offset));
  39. }
  40. public function offsetSet($offset, $value): void
  41. {
  42. parent::offsetSet(str_rot13($offset), $value);
  43. }
  44. }
  45. $values = ['foo' => '', 'bar' => null, 'baz' => 42];
  46. echo "==== class with offsetExists() and offsetGet() ====\n";
  47. $object = new ArrayObjectBoth($values);
  48. var_dump($object->offsetExists('foo'), isset($object['foo']), empty($object['foo']));
  49. var_dump($object->offsetExists('bar'), isset($object['bar']), empty($object['bar']));
  50. var_dump($object->offsetexists('baz'), isset($object['baz']), empty($object['baz']));
  51. var_dump($object->offsetexists('qux'), isset($object['qux']), empty($object['qux']));
  52. echo "==== class with offsetExists() ====\n";
  53. $object = new ArrayObjectExists($values);
  54. var_dump($object->offsetExists('foo'), isset($object['foo']), empty($object['foo']));
  55. var_dump($object->offsetExists('bar'), isset($object['bar']), empty($object['bar']));
  56. var_dump($object->offsetexists('baz'), isset($object['baz']), empty($object['baz']));
  57. var_dump($object->offsetexists('qux'), isset($object['qux']), empty($object['qux']));
  58. echo "==== class with offsetGet() ====\n";
  59. $object = new ArrayObjectGet($values);
  60. var_dump($object->offsetExists('foo'), isset($object['foo']), empty($object['foo']));
  61. var_dump($object->offsetExists('bar'), isset($object['bar']), empty($object['bar']));
  62. var_dump($object->offsetexists('baz'), isset($object['baz']), empty($object['baz']));
  63. var_dump($object->offsetexists('qux'), isset($object['qux']), empty($object['qux']));
  64. echo "==== class with offsetGet() and offsetSet() ====\n";
  65. $object = new ArrayObjectGetSet;
  66. $object['foo'] = 42;
  67. var_dump($object->offsetExists('foo'), $object->offsetExists('sbb'), isset($object['foo']), isset($object['sbb']), empty($object['sbb']));
  68. ?>
  69. --EXPECTF--
  70. ==== class with offsetExists() and offsetGet() ====
  71. string(37) "Called: ArrayObjectBoth::offsetExists"
  72. string(37) "Called: ArrayObjectBoth::offsetExists"
  73. string(37) "Called: ArrayObjectBoth::offsetExists"
  74. string(34) "Called: ArrayObjectBoth::offsetGet"
  75. bool(true)
  76. bool(true)
  77. bool(true)
  78. string(37) "Called: ArrayObjectBoth::offsetExists"
  79. string(37) "Called: ArrayObjectBoth::offsetExists"
  80. string(37) "Called: ArrayObjectBoth::offsetExists"
  81. string(34) "Called: ArrayObjectBoth::offsetGet"
  82. bool(true)
  83. bool(true)
  84. bool(true)
  85. string(37) "Called: ArrayObjectBoth::offsetExists"
  86. string(37) "Called: ArrayObjectBoth::offsetExists"
  87. string(37) "Called: ArrayObjectBoth::offsetExists"
  88. string(34) "Called: ArrayObjectBoth::offsetGet"
  89. bool(true)
  90. bool(true)
  91. bool(false)
  92. string(37) "Called: ArrayObjectBoth::offsetExists"
  93. string(37) "Called: ArrayObjectBoth::offsetExists"
  94. string(37) "Called: ArrayObjectBoth::offsetExists"
  95. bool(false)
  96. bool(false)
  97. bool(true)
  98. ==== class with offsetExists() ====
  99. string(39) "Called: ArrayObjectExists::offsetExists"
  100. string(39) "Called: ArrayObjectExists::offsetExists"
  101. string(39) "Called: ArrayObjectExists::offsetExists"
  102. bool(true)
  103. bool(true)
  104. bool(true)
  105. string(39) "Called: ArrayObjectExists::offsetExists"
  106. string(39) "Called: ArrayObjectExists::offsetExists"
  107. string(39) "Called: ArrayObjectExists::offsetExists"
  108. bool(true)
  109. bool(true)
  110. bool(true)
  111. string(39) "Called: ArrayObjectExists::offsetExists"
  112. string(39) "Called: ArrayObjectExists::offsetExists"
  113. string(39) "Called: ArrayObjectExists::offsetExists"
  114. bool(true)
  115. bool(true)
  116. bool(false)
  117. string(39) "Called: ArrayObjectExists::offsetExists"
  118. string(39) "Called: ArrayObjectExists::offsetExists"
  119. string(39) "Called: ArrayObjectExists::offsetExists"
  120. bool(false)
  121. bool(false)
  122. bool(true)
  123. ==== class with offsetGet() ====
  124. string(33) "Called: ArrayObjectGet::offsetGet"
  125. bool(true)
  126. bool(true)
  127. bool(true)
  128. string(33) "Called: ArrayObjectGet::offsetGet"
  129. bool(true)
  130. bool(false)
  131. bool(true)
  132. string(33) "Called: ArrayObjectGet::offsetGet"
  133. bool(true)
  134. bool(true)
  135. bool(false)
  136. bool(false)
  137. bool(false)
  138. bool(true)
  139. ==== class with offsetGet() and offsetSet() ====
  140. Warning: Undefined array key "foo" in %s on line %d
  141. bool(false)
  142. bool(true)
  143. bool(false)
  144. bool(true)
  145. bool(true)