Must not use parent::$%s::%s() outside a property hook¶
Description¶
Property hooks are actual methods: they can call other methods to fulfill their mission. They may also call the parent’ methods, and, as such, they may also call the parent’s property hook. This has lead to the syntax parent::$properpty::get()
.
This syntax is specific to property hooks, and can only be used inside the same property hook for the same property.
Example¶
<?php
class X {
public string $q {
get => 'in parent';
}
}
class Y extends X {
public string $q {
get => parent::$q::get();
}
function foo(): string {
// Must not use parent::$q::get() outside a property hook
return parent::$q::get();
}
}
$objectY = new X;
echo $objectY->q;
?>
Literal Examples¶
Must not use parent::$q::get() outside a property hook
Solutions¶
Move the block of the property hook in a regular method and call it from the hook and other methods.
Inline the code every time it is needed.