Re: [RFC Discussion] Typed Properties
| From: | Yasuo Ohgaki | Date: | Fri, 18 Mar 2016 10:55:20 +0000 |
| Subject: | Re: [RFC Discussion] Typed Properties | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-91746@lists.php.net to get a copy of this message | ||
Hi,
On Thu, Mar 17, 2016 at 8:46 PM, Andrey Andreev <narf@devilix.net> wrote:
> I'd support borrowing the "?" nullable annotation from HackLang for
>> people who want a less strict behavior:
>>
>> public ?string $name;
>>
>> This means that $name can either be a string or the NULL value.
>>
>
> Or, do it like with parameter type hinting:
>
> public string $name = null;
>
> Although I'm all for limiting NULL to only being the default value:
>
> $this->name = null; // this should throw a TypeError
NULL is special type. '' and NULL is different entity.
class User {
public string $username = NULL;
}
After authentication $username could be "yohgaki". What is should be
after logout? $username = '' does not make sense, but $username =
NULL.
There are ways to work around this, but property cannot be NULL is
problematic. IMO.
class User {
public string|null $username = NULL;
}
may work. I'm not sure if this is good idea in general.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net