On Wed, Sep 19, 2018 at 10:04 PM Levi Morrison <le...@php.net> wrote:

> I think this code should be allowed:
>
>    class User {
>         public int $id;
>         public string $preferred_name;
>         public string $username;
>     }

This code is broken - by making the properties non-nullable, you're
literally saying "these properties will be initialized", then
proceeding to not initialize them. That's just incomplete code.

Note that we're talking about two different things here - I was
talking about bypassing declared constructors for technical reasons.

You're talking about omitting constructors from the declaration - but
all classes have a constructor. Not declaring it just implies an empty
constructor. You can even invoke it using reflection.

For your use-case, assuming you insist on writing bad code, is more
accurate like this:

    class User {
         public ?int $id;
         public ?string $preferred_name;
         public ?string $username;
     }

These properties are valid without a constructor. You can safely infer
them as null, rather than as "uninitialized".

Non-nullable properties aren't valid without initialization. Not in
any language I've ever heard of.

Maybe PHP has to be the first to prove every other language right?

We have enough half-baked features and inconsistencies as it is.

We'll regret this forever. Like how Javascript developers regret
having null and undefined on a daily basis.

Uninitialized is the new null - it's the PHP equivalent of undefined
in Javascript.

Please no.

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

Reply via email to