Re: [RFC][Vote] Typed Properties
| From: | Rowan Collins | Date: | Thu, 26 May 2016 11:57:39 +0000 |
| Subject: | Re: [RFC][Vote] Typed Properties | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-93561@lists.php.net to get a copy of this message | ||
On 26/05/2016 11:49, Fleshgrinder wrote:
On 5/26/2016 12:39 PM, Rowan Collins wrote:Heh, no worries, easily done. :)On 26/05/2016 11:16, Fleshgrinder wrote:Now I feel stupid but I guess I got lost myself. :P$o = (object) ['x' => null];Apart from a typo in your example (you change from $o to $a), this is already the current behaviour, and always has been: https://3v4l.org/NeqGl isset() is really very simple: if the thing your accessing would give you the value null, it returns false, otherwise it returns true. Regards,var_dump(isset($a->x)); // falsevar_dump(property_exists($a, 'x')); // true
This means it is even simpler, we just have to add the E_NOTICE and be done with it. $g = new class { public $x; };I am definitely in favour of this from a language point of view. The notice is warning about *initialization*, not *declaration*, so it makes sense to warn on "var $foo" and not "var $foo = null". The only thing I'm not sure is what the impact would have on the internals, which may be why it doesn't already work that way. With a simple variable the notice can be easily raised based on the variable name not being in the current symbol table; with an object property, the name *is* in the relevant table, because it's been *declared* even though it hasn't been *initialized*. Thus you need some extra flag to track that a declared property exists but has never been written to. But it sounds like typed properties require adding some such overhead anyway... Regards, -- Rowan Collins [IMSoP]var_dump($g->x); // null + E_NOTICE Uninitialized var_dump(isset($g->x)); // falsevar_dump(property_exists($g, 'x')); // truevar_dump(is_null($g->x)); // true + E_NOTICE Uninitialized var_dump($g->x == null); // true + E_NOTICE Uninitialized var_dump($g->x === null); // true + E_NOTICE UninitializedThis behavior would be true for all variations: class A {var $x;public $x;public int $x;public ?int $x;} No notice for the following: class A {var $x = null;public $x = null;public int $x = 0;public ?int $x = null;}