Re: 'Uninitialized' creates a lot of cluttering
| From: | naitsirch at e dot mail dot de | Date: | Wed, 08 Feb 2023 21:35:51 +0000 |
| Subject: | Re: 'Uninitialized' creates a lot of cluttering | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119498@lists.php.net to get a copy of this message | ||
Am 08-Feb-2023 17:34:43 +0100 schrieb tim@bastelstu.be:
> 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.
That's a valid point. Thanks for the hint.
>
> 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
Best regards
Christian