readonly_modification.phpt 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. --TEST--
  2. Modifying a readonly property
  3. --FILE--
  4. <?php
  5. class Test {
  6. readonly public int $prop;
  7. readonly public array $prop2;
  8. public function __construct() {
  9. // Initializing assignments.
  10. $this->prop = 1;
  11. $this->prop2 = [];
  12. }
  13. }
  14. function byRef(&$ref) {}
  15. $test = new Test;
  16. var_dump($test->prop); // Read.
  17. try {
  18. $test->prop = 2;
  19. } catch (Error $e) {
  20. echo $e->getMessage(), "\n";
  21. }
  22. try {
  23. $test->prop += 1;
  24. } catch (Error $e) {
  25. echo $e->getMessage(), "\n";
  26. }
  27. try {
  28. $test->prop++;
  29. } catch (Error $e) {
  30. echo $e->getMessage(), "\n";
  31. }
  32. try {
  33. ++$test->prop;
  34. } catch (Error $e) {
  35. echo $e->getMessage(), "\n";
  36. }
  37. try {
  38. $ref =& $test->prop;
  39. } catch (Error $e) {
  40. echo $e->getMessage(), "\n";
  41. }
  42. try {
  43. $test->prop =& $ref;
  44. } catch (Error $e) {
  45. echo $e->getMessage(), "\n";
  46. }
  47. try {
  48. byRef($test->prop);
  49. } catch (Error $e) {
  50. echo $e->getMessage(), "\n";
  51. }
  52. var_dump($test->prop2); // Read.
  53. try {
  54. $test->prop2[] = 1;
  55. } catch (Error $e) {
  56. echo $e->getMessage(), "\n";
  57. }
  58. try {
  59. $test->prop2[0][] = 1;
  60. } catch (Error $e) {
  61. echo $e->getMessage(), "\n";
  62. }
  63. ?>
  64. --EXPECT--
  65. int(1)
  66. Cannot modify readonly property Test::$prop
  67. Cannot modify readonly property Test::$prop
  68. Cannot modify readonly property Test::$prop
  69. Cannot modify readonly property Test::$prop
  70. Cannot modify readonly property Test::$prop
  71. Cannot modify readonly property Test::$prop
  72. Cannot modify readonly property Test::$prop
  73. array(0) {
  74. }
  75. Cannot modify readonly property Test::$prop2
  76. Cannot modify readonly property Test::$prop2