On Sat, Jun 5, 2021 at 11:47 AM Pierre <pierre-...@processus.org> wrote:
> Le 04/06/2021 à 17:19, Nikita Popov a écrit : > > Hi internals, > > I'd like to open the discussion on readonly > properties:https://wiki.php.net/rfc/readonly_properties_v2 > > This proposal is similar to thehttps://wiki.php.net/rfc/write_once_properties > RFC that has been declined > previously. One significant difference is that the new RFC limits the scope > of initializing assignments. I think a key mistake of the previous RFC was > the confusing "write-once" framing, which is both technically correct and > quite irrelevant. > > Please see the rationale section > (https://wiki.php.net/rfc/readonly_properties_v2#rationale) for how this > proposal relates to other RFCs and alternatives. > > Regards, > Nikita > > > Hello, > > 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. > Yes, this is supported. What matters is the scope from which the initialization occurs. If you rebind a closure to the scope of the class, then you can perform any operations that would be legal inside that class, including access to private properties and initialization of readonly properties. Regards, Nikita