variation.phpt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. --TEST--
  2. Readonly variations
  3. --FILE--
  4. <?php
  5. class Test {
  6. public readonly int $prop;
  7. public function init() {
  8. $this->prop = 1;
  9. }
  10. public function r() {
  11. echo $this->prop;
  12. }
  13. public function w() {
  14. $this->prop = 1;
  15. echo 'done';
  16. }
  17. public function rw() {
  18. $this->prop += 1;
  19. echo 'done';
  20. }
  21. public function im() {
  22. $this->prop[] = 1;
  23. echo 'done';
  24. }
  25. public function is() {
  26. echo (int) isset($this->prop);
  27. }
  28. public function us() {
  29. unset($this->prop);
  30. echo 'done';
  31. }
  32. }
  33. function r($test) {
  34. echo $test->prop;
  35. }
  36. function w($test) {
  37. $test->prop = 0;
  38. echo 'done';
  39. }
  40. function rw($test) {
  41. $test->prop += 1;
  42. echo 'done';
  43. }
  44. function im($test) {
  45. $test->prop[] = 1;
  46. echo 'done';
  47. }
  48. function is($test) {
  49. echo (int) isset($test->prop);
  50. }
  51. function us($test) {
  52. unset($test->prop);
  53. echo 'done';
  54. }
  55. foreach ([true, false] as $init) {
  56. foreach ([true, false] as $scope) {
  57. foreach (['r', 'w', 'rw', 'im', 'is', 'us'] as $op) {
  58. $test = new Test();
  59. if ($init) {
  60. $test->init();
  61. }
  62. echo 'Init: ' . ((int) $init) . ', scope: ' . ((int) $scope) . ', op: ' . $op . ": ";
  63. try {
  64. if ($scope) {
  65. $test->{$op}();
  66. } else {
  67. $op($test);
  68. }
  69. } catch (Error $e) {
  70. echo $e->getMessage();
  71. }
  72. echo "\n";
  73. }
  74. }
  75. }
  76. ?>
  77. --EXPECT--
  78. Init: 1, scope: 1, op: r: 1
  79. Init: 1, scope: 1, op: w: Cannot modify readonly property Test::$prop
  80. Init: 1, scope: 1, op: rw: Cannot modify readonly property Test::$prop
  81. Init: 1, scope: 1, op: im: Cannot modify readonly property Test::$prop
  82. Init: 1, scope: 1, op: is: 1
  83. Init: 1, scope: 1, op: us: Cannot unset readonly property Test::$prop
  84. Init: 1, scope: 0, op: r: 1
  85. Init: 1, scope: 0, op: w: Cannot modify readonly property Test::$prop
  86. Init: 1, scope: 0, op: rw: Cannot modify readonly property Test::$prop
  87. Init: 1, scope: 0, op: im: Cannot modify readonly property Test::$prop
  88. Init: 1, scope: 0, op: is: 1
  89. Init: 1, scope: 0, op: us: Cannot unset readonly property Test::$prop
  90. Init: 0, scope: 1, op: r: Typed property Test::$prop must not be accessed before initialization
  91. Init: 0, scope: 1, op: w: done
  92. Init: 0, scope: 1, op: rw: Typed property Test::$prop must not be accessed before initialization
  93. Init: 0, scope: 1, op: im: Cannot indirectly modify readonly property Test::$prop
  94. Init: 0, scope: 1, op: is: 0
  95. Init: 0, scope: 1, op: us: done
  96. Init: 0, scope: 0, op: r: Typed property Test::$prop must not be accessed before initialization
  97. Init: 0, scope: 0, op: w: Cannot initialize readonly property Test::$prop from global scope
  98. Init: 0, scope: 0, op: rw: Typed property Test::$prop must not be accessed before initialization
  99. Init: 0, scope: 0, op: im: Cannot indirectly modify readonly property Test::$prop
  100. Init: 0, scope: 0, op: is: 0
  101. Init: 0, scope: 0, op: us: Cannot unset readonly property Test::$prop from global scope