Property Hooks And Default Values¶
By Jan Nedbal
<?php declare(strict_types = 1);
class Example1
{
public string $foo = 'UPPERCASE' {
set => strtolower($value);
}
}
class Example2
{
public function __construct(
public string $foo = 'UPPERCASE' {
set => strtolower($value);
}
) {
}
}
echo new Example1()->foo;
echo new Example2()->foo;
PHP property hooks
Would you expect equal behaviour?
[Added answer] The default value on the property hook is used when the hook is called. At creation time, no hook is called to initialize the property. So, there is no default value for properties with a property hook, unless the set hook is configured to return something.
See Also¶
PHP Features¶
Last updated: 14 July 2026