Le 04/06/2021 à 17:19, Nikita Popov a écrit :
If I understood it well you count assignments, one note thought after re-reading once again, you state:
This variant is not allowed, as the initializing assignment occurs from outside the class:
Does this mean it'll prevent programmatic object hydration using the scope-stealing closure pattern ? Such as:
<?php
class SomeClass
{
public readonly int $foo;
}
$bar = 12;
$object = (new \ReflectionClass('SomeClass'))->newInstanceWithoutConstructor();
(\Closure::bind(
function (SomeClass $object) ($bar
$object->foo = $bar;
},
null,
SomeClass::class
))($object);
?>
I know this is a weird stuff to do I wouldn't advise to others myself, but some hydration libraries such as ocramius/generated-hydrator do leverage this mechanism to hydrate objects, and I sometime do it myself in business code, sporadically, to hydrate objects from database without needing to define a constructor for those value objects.
If it does prevent such code, will there be some additional mechanism to create value objects with readonly properties without constructor ? Such as an JavaScript object-like object instanciation mechanism ?
<?php
class SomeClass
{
public readonly int $foo;
}
$bar = 12;
$object = {
foo: $bar,
};
?>
I know this goes far outside of this RFC's own scope, but maybe this is the occasion to open a discussion about this, this would play well with readonly properties and various serializers or hydrators libraries.
Regards,
--
Pierre