Re: [RFC] [VOTE] Typed properties v2
| From: | Rowan Collins | Date: | Sun, 23 Sep 2018 20:55:06 +0000 |
| Subject: | Re: [RFC] [VOTE] Typed properties v2 | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-103242@lists.php.net to get a copy of this message | ||
On 23/09/2018 19:31, David Rodrigues wrote:
Em dom, 23 de set de 2018 às 13:09, Larry Garfield <larry@garfieldtech.com> escreveu:Yes, there are various cases where checking at the end of the constructor is not enough. The general consensus is that we can't catch all of them without major changes to the language, but can catch some of the more common scenarios earlier than the current implementation does.3) Object properties may be type hinted and the class author has until the end of the constructor to make sure they're fulfilled, otherwise TypeError on the spot (what I'm proposing).Item 3 could not be enough, for instance: <<< class Example {public /* string */ $string; public function __construct() { static:: stringParameter($this->string); }public static function stringParameter(string $string) {}} new Example; It will fail because __construct() uses an uninitialized non-nullable property.
Maybe it should throw a new Exception like UninitializedPropertyException when you try to read a uninitialized property like this case.That's exactly what the current implementation will do, and what Larry's proposed addition would continue doing in cases where it can't be spotted at a "checkpoint" like the end of the constructor. Regards, -- Rowan Collins [IMSoP]