diff --git a/language/oop5/property-hooks.xml b/language/oop5/property-hooks.xml index 05637fb9751a..d66568b61517 100644 --- a/language/oop5/property-hooks.xml +++ b/language/oop5/property-hooks.xml @@ -199,6 +199,72 @@ class Example On a backed property, omitting a get orset hook means the default read or write behavior will be used. + + + Hooks can be defined when using + constructor property promotion. + However, when doing so, values provided + to the constructor must match the type associated with the property, + regardless of what the set hook might allow. + + + Consider the following: + + +created = $value; + } + }, + ) { + } +} +]]> + + + Internally, the engine decomposes this to the following: + + +created = $value; + } + } + + public function __construct( + DateTimeInterface $created, + ) { + $this->created = $created; + } +} +]]> + + + Any attempts to set the property outside the constructor will + allow either string or DateTimeInterface + values, but the constructor will only allow DateTimeInterface. + This is because the defined type for the property (DateTimeInterface) + is used as the parameter type within the constructor signature, regardless of what + the set hook allows. + + + If this kind of behavior is needed from the constructor, constructor + property promotion cannot be used. + + Virtual properties