Re: 'Uninitialized' creates a lot of cluttering

From: Date: Wed, 08 Feb 2023 16:34:39 +0000
Subject: Re: 'Uninitialized' creates a lot of cluttering
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-119491@lists.php.net to get a copy of this message
Hi On 2/8/23 17:04, naitsirch@e.mail.de wrote:
Am 08-Feb-2023 15:59:02 +0100 schrieb tekiela246@gmail.com:
When using typed properties, the language cannot use NULL as the default anymore because the type might not allow NULL, e.g public string $name allows only string values.
Would it make sense to make "null" the default value for nullable properties at least? So that one could write class Test {
     public ?string $name;
} var_dump((new Test())->name); // null
No, I find the difference between "null" and "uninitialized" useful, because it makes the behavior explicit. In case I make a mistake and accidentally don't assign a value to the property when I should've, perhaps I've forgot to call the necessary setter in my constructor. If I later access the property it will blow up instead of silently feeding me garbage data. Adding special logic for nullable properties to save the developer from typing the 7 characters '= null;' in some rare cases, does not sound useful to me. Best regards Tim Düsterhus

« previous php.internals (#119491) next »