Hooked properties cannot be readonly

Description

The readonly keyword is not allowed on hooked properties. On the other hand, it is possible to emulate that feature, by writing the set method.

Example

<?php

class X {
    public readonly string $x {
     get => $this->x;

     set { if (!isset($this->x)) $this->x = $value;}
    }
}

?>

Solutions

  • Use the set hook to emulate readonly.

  • Omit the readonly keyword.