Re: [RFC] Readonly properties
| From: | Benjamin Eberlei | Date: | Sat, 05 Jun 2021 14:28:27 +0000 |
| Subject: | Re: [RFC] Readonly properties | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-114738@lists.php.net to get a copy of this message | ||
On Fri, Jun 4, 2021 at 5:20 PM Nikita Popov <nikita.ppv@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']);
>
> Regards,
> Nikita
>