variation_nested.phpt 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. --TEST--
  2. Readonly nested variations
  3. --FILE--
  4. <?php
  5. class Inner {
  6. public int $prop = 1;
  7. public array $array = [];
  8. }
  9. class Test {
  10. public readonly Inner $prop;
  11. public function init() {
  12. $this->prop = new Inner();
  13. }
  14. }
  15. function r($test) {
  16. echo $test->prop->prop;
  17. }
  18. function w($test) {
  19. $test->prop->prop = 0;
  20. echo 'done';
  21. }
  22. function rw($test) {
  23. $test->prop->prop += 1;
  24. echo 'done';
  25. }
  26. function im($test) {
  27. $test->prop->array[] = 1;
  28. echo 'done';
  29. }
  30. function is($test) {
  31. echo (int) isset($test->prop->prop);
  32. }
  33. function us($test) {
  34. unset($test->prop->prop);
  35. echo 'done';
  36. }
  37. foreach ([true, false] as $init) {
  38. foreach (['r', 'w', 'rw', 'im', 'is', 'us'] as $op) {
  39. $test = new Test();
  40. if ($init) {
  41. $test->init();
  42. }
  43. echo 'Init: ' . ((int) $init) . ', op: ' . $op . ": ";
  44. try {
  45. $op($test);
  46. } catch (Error $e) {
  47. echo $e->getMessage();
  48. }
  49. echo "\n";
  50. }
  51. }
  52. ?>
  53. --EXPECT--
  54. Init: 1, op: r: 1
  55. Init: 1, op: w: done
  56. Init: 1, op: rw: done
  57. Init: 1, op: im: done
  58. Init: 1, op: is: 1
  59. Init: 1, op: us: done
  60. Init: 0, op: r: Typed property Test::$prop must not be accessed before initialization
  61. Init: 0, op: w: Cannot indirectly modify readonly property Test::$prop
  62. Init: 0, op: rw: Typed property Test::$prop must not be accessed before initialization
  63. Init: 0, op: im: Cannot indirectly modify readonly property Test::$prop
  64. Init: 0, op: is: 0
  65. Init: 0, op: us: done