Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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;}
    }
}

?>

Alternatives

  • Use the set hook to emulate readonly.
  • Omit the readonly keyword.
  • Remove the property hooks from the property definition.