fixedarray_002.phpt 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. --TEST--
  2. SPL: FixedArray: overloading
  3. --FILE--
  4. <?php
  5. class A extends SplFixedArray {
  6. public function count() {
  7. return 2;
  8. }
  9. public function offsetGet($n) {
  10. echo "A::offsetGet\n";
  11. return parent::offsetGet($n);
  12. }
  13. public function offsetSet($n, $v) {
  14. echo "A::offsetSet\n";
  15. return parent::offsetSet($n, $v);
  16. }
  17. public function offsetUnset($n) {
  18. echo "A::offsetUnset\n";
  19. return parent::offsetUnset($n);
  20. }
  21. public function offsetExists($n) {
  22. echo "A::offsetExists\n";
  23. return parent::offsetExists($n);
  24. }
  25. }
  26. $a = new A;
  27. // errors
  28. try {
  29. $a[0] = "value1";
  30. } catch (RuntimeException $e) {
  31. echo "Exception: ".$e->getMessage()."\n";
  32. }
  33. try {
  34. var_dump($a["asdf"]);
  35. } catch (RuntimeException $e) {
  36. echo "Exception: ".$e->getMessage()."\n";
  37. }
  38. try {
  39. unset($a[-1]);
  40. } catch (RuntimeException $e) {
  41. echo "Exception: ".$e->getMessage()."\n";
  42. }
  43. $a->setSize(10);
  44. $a[0] = "value0";
  45. $a[1] = "value1";
  46. $a[2] = "value2";
  47. $a[3] = "value3";
  48. $ref = "value4";
  49. $ref2 =&$ref;
  50. $a[4] = $ref;
  51. $ref = "value5";
  52. unset($a[1]);
  53. var_dump(isset($a[1]), isset($a[2]), empty($a[1]), empty($a[2]));
  54. var_dump($a[0], $a[2], $a[3], $a[4]);
  55. // countable
  56. var_dump(count($a), $a->getSize(), count($a) == $a->getSize());
  57. ?>
  58. ===DONE===
  59. --EXPECTF--
  60. A::offsetSet
  61. Exception: Index invalid or out of range
  62. A::offsetGet
  63. Exception: Index invalid or out of range
  64. A::offsetUnset
  65. Exception: Index invalid or out of range
  66. A::offsetSet
  67. A::offsetSet
  68. A::offsetSet
  69. A::offsetSet
  70. A::offsetSet
  71. A::offsetUnset
  72. A::offsetExists
  73. A::offsetExists
  74. A::offsetExists
  75. A::offsetExists
  76. bool(false)
  77. bool(true)
  78. bool(true)
  79. bool(false)
  80. A::offsetGet
  81. A::offsetGet
  82. A::offsetGet
  83. A::offsetGet
  84. string(6) "value0"
  85. string(6) "value2"
  86. string(6) "value3"
  87. string(6) "value4"
  88. int(2)
  89. int(10)
  90. bool(false)
  91. ===DONE===