Hi

On 2/8/23 17:04, naitsi...@e.mail.de wrote:
Am 08-Feb-2023 15:59:02 +0100 schrieb tekiela...@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

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

Reply via email to