Re: [RFC] Undefined Property Error Promotion
| From: | Robert Landers | Date: | Wed, 06 Apr 2022 12:37:25 +0000 |
| Subject: | Re: [RFC] Undefined Property Error Promotion | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117482@lists.php.net to get a copy of this message | ||
On Wed, Apr 6, 2022 at 2:04 PM Mark Randall <marandall@php.net> wrote:
>
> Internals,
>
> Part 2 of the undefined behaviour improvements, this time focusing on
> properties.
>
> https://wiki.php.net/rfc/undefined_property_error_promotion
>
> This RFC draws heavily from the just passed undefined variables error
> promotion RFC, and is intended to compliment both it, and the 8.2
> Deprecate Dynamic Properties RFC.
>
> The arguments in favour are the same as the last one, reading things
> which don't exist will often lead to the program entering an unintended
> state, and is likely the result of a programming error.
>
> There is a difference though that we do explicitly provide an object
> that is designed to be dynamic, and that is stdClass which is the
> typical output from json_decode and array to object casts.
>
> I would expect we might want to discuss special-casing the accessing of
> properties on stdClass and leave them as a warning.
>
> However, I personally think that for the sake of consistency we should
> make undefined properties throw across the board, including stdClass.
>
> We already have fully backwards compatible mechanisms built into the
> language (isset, empty, null coalesce, property_exists) to safely handle
> cases of the property not being defined, even on objects that do not
> have a fixed structure.
>
> I was originally going to include a section for discussion about
> potentially using AllowDynamicProperties to pull double duty, allowing
> reads without an error as well, but I do not believe that would be in
> the best interests of the language, and so removed it.
>
> Mark Randall
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
Hey Mark,
This looks awesome!
FWIW, I'd like to see option 2 only because of custom serializers
and/or object proxies and also because:
> This RFC proposes that accessing an undefined property is rendered illegal behaviour
StdClass has *no* defined properties and thus would always throw. But
I also guess it depends on what you define "undefined" as. Does simply
doing $obj->prop "define" the property "prop"?
-- Rob