-
-
Notifications
You must be signed in to change notification settings - Fork 112
Description
Version: 3.0.20
Bug Description
It seems that when I use an object for template parameters, property hooks are not properly supported at this moment.
Steps To Reproduce
class Params
{
public function __construct(public string $firstName, public string $lastName) {}
public string $fullName { get => $this->firstName . ' ' . $this->lastName; }
}
$params = new Params('John', 'Doe');
$html = $latte->renderToString('template.latte', $params);Template:
{$fullName}This throws Undefined variable $fullName.
Expected Behavior
No error, display the value.
Possible Solution
I believe this code is the culprit because it casts the object to an array, which omits the virtual property.
As per the property hooks RFC, this seems to be by design:
When casting an object to an array (
$arr = (array) $obj), currently the visibility of properties is ignored; the keys returned may have an extra prefix in them to indicate that they were private, but that's it. As this operation currently reveals internal implementation details, it also will not invoke the get hook.
This might also have implications for non-virtual properties with a get hook, because I would intuitively expect the hook to be used when I reference the variable in the template.
Perhaps the code should use reflection to list public properties and get their values in a way that does not bypass the get hook?