fixedarray_002.phpt 1.9 KB

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