On Sat, Jun 5, 2021 at 4:28 PM Benjamin Eberlei <kont...@beberlei.de> wrote:
> > > On Fri, Jun 4, 2021 at 5:20 PM Nikita Popov <nikita....@gmail.com> wrote: > >> 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 the >> https://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. >> > > I have a question about Reflection support, the RFC states: > > > ReflectionProperty::setValue() can bypass the requirement that > initialization occurs from the scope where the property has been declared. > However, reflection cannot modify a readonly property that has already been > initialized. > > Is there a reason why this is not possible? I am thinking about ORMs or > Deserializers here where a pattern would be: > > class MyDataObject > { > public function __construct( > public readonly $foo > ) {} > } > > $dataObject = $reflectionClass->newInstanceWithoutConstructor(); > $dataObject->getProperty('foo')->setValue($row['value']); > This example is compatible with the proposal. Calling newInstanceWithoutConstructor() leaves the property uninitialized, and you can initialize it through reflection (or closure rebinding) from any scope. Hydration and deserialization only requires the ability to initialize, not to change the value of properties past initialization. Regards, Nikita