On 11/05/2021 14:12, Dik Takken wrote:
So the use of "new" in initializers would require extending this base
class and call the parent constructor. Maybe forgetting to do so could
throw, preventing initializers from silently not working.


If we managed to get that working, I wonder if we could also include a check for uninitialized typed properties, a bit like Swift's "two-phase initializers". In short, make this throw an error:

class Foo extends \PHP\Base {
    private Logger $logger;

    public function __construct() {
        parent::construct(); // "Error: Property $logger has no default and was not initialized before calling Base constructor"
    }
}

This would catch bugs closer to their source, where currently there is no error until $logger is accessed.


However, the "if" at the beginning of this message is quite a big one: it's not obvious where to assert any of this, because the constructor is just called as a normal method *after* the engine considers the object to be "created".


Regards,

--
Rowan Tommins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to