Re: [RFC][Vote] Typed Properties
| From: | Tom Worster | Date: | Thu, 26 May 2016 16:40:29 +0000 |
| Subject: | Re: [RFC][Vote] Typed Properties | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-93571@lists.php.net to get a copy of this message | ||
On 5/26/16, 12:30 PM, "Fleshgrinder" <php@fleshgrinder.com> wrote:
>The problem is a completely different one, how should the following code
>behave?
>
> class A {
>
> public int $x;
>
> }
>
> (new A)->x;
>
>The property has no value assigned but it is being accessed. The current
>PHP behavior is to simply initialize it with null. But this is
>impossible according to the type definition.
>
>There are not many ways to handle this. I think we already had all of
>them proposed:
>
>0. Fatal error after __construct was called.
>1. Fatal error and abort.
>2. Initialize with appropriate type.
>3. Initialize with null.
Under another 5th option, the problem you state does not arise. Disallow
"public int $x;". Under this option you may declare $x with type int and
an initial value or you may declare $x without type but you may not
declare $x with type (nullable or not) and undefined initial value.
Tom