unset.phpt 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. --TEST--
  2. Unset readonly property
  3. --FILE--
  4. <?php
  5. class Test {
  6. public readonly int $prop;
  7. public function __construct(int $prop) {
  8. $this->prop = $prop;
  9. }
  10. }
  11. $test = new Test(1);
  12. try {
  13. unset($test->prop);
  14. } catch (Error $e) {
  15. echo $e->getMessage(), "\n";
  16. }
  17. class Test2 {
  18. public readonly int $prop;
  19. public function __construct() {
  20. unset($this->prop); // Unset uninitialized.
  21. unset($this->prop); // Unset unset.
  22. }
  23. public function __get($name) {
  24. // Lazy init.
  25. echo __METHOD__, "\n";
  26. $this->prop = 1;
  27. return $this->prop;
  28. }
  29. }
  30. $test = new Test2;
  31. var_dump($test->prop); // Call __get.
  32. var_dump($test->prop); // Don't call __get.
  33. try {
  34. unset($test->prop); // Unset initialized, illegal.
  35. } catch (Error $e) {
  36. echo $e->getMessage(), "\n";
  37. }
  38. class Test3 {
  39. public readonly int $prop;
  40. }
  41. $test = new Test3;
  42. try {
  43. unset($test->prop);
  44. } catch (Error $e) {
  45. echo $e->getMessage(), "\n";
  46. }
  47. ?>
  48. --EXPECT--
  49. Cannot unset readonly property Test::$prop
  50. Test2::__get
  51. int(1)
  52. int(1)
  53. Cannot unset readonly property Test2::$prop
  54. Cannot unset readonly property Test3::$prop from global scope