Re: [VOTE] Undefined Property Error Promotion
| From: | Rowan Tommins | Date: | Mon, 25 Apr 2022 16:19:33 +0000 |
| Subject: | Re: [VOTE] Undefined Property Error Promotion | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117600@lists.php.net to get a copy of this message | ||
On 25/04/2022 12:24, Guilliam Xavier wrote:
Hi Rowan, (...) Also, there's this non-standard class calledIndeed, and if that was the only exception, it would be reasonable; it's when you pile it on top of all the other rules that it starts making people sad."stdClass", which unlike standard classes, you can read and write any property you like without declaring it,Better reordered "write and read" I think. Yes, like associative arrays, stdClass is "dynamic" by design.
You're right, I was mis-remembering; the RFC explicitly says that this will error.and even if you use unset(), you'll never get an error.That's not true, e.g. given$o = (object)['foo' => 1]; $o->bar = 2;thenunset($o->foo, $o->bar);, trying to read$o->fooor$o->barwill both cause an error (like for$o->qux); or I'm misunderstanding you?
Are you proposing to deprecate the implicit = null default initializer
for untyped properties?
Yes.
Thanks for the recap. For untyped properties, that's the historical behavior. For typed properties, that's explained in https://wiki.php.net/rfc/typed_properties_v2#uninitialized_and_unset_properties *. What's the issue?The issue is that for new users, saying "that's the historical behaviour, you just have to learn it" is not very helpful.
* "If a typed property is unset(), then it returns to the uninitialized
state. While we would love to remove support for the unsetting of
properties, this functionality is currently used for lazy initialization by
Doctrine, in combination with the functionality described in the following
section."
I think both you and Mark misunderstood my point in bringing this up - I'm not complaining about the ability to call unset(), I'm complaining that it behaves differently depending how the property was declared.
Look at how the untyped property behaves in this example: https://3v4l.org/nClNs The property disappears from var_dump(), but is not actually deleted from the object, as seen by it still being private when assigned a new value. Given that we have the magic "uninitialized" state, and we're proposing to make reading an unset property an error, it would make more sense for it to show as '["untyped":"Foo":private] => uninitialized(mixed)'
Regards,
--
Rowan Tommins
[IMSoP]