Hi Kamil,

Thanks for your reply.
With regards to the automatic default, of course I can only agree!

Indeed isset() should not be needed for declared properties!
But because of the typed properties, you have to initialize them before you
can access them. I think that is cluttering up the code.

>From a programmer's perspective, the property is declared and should be
accessible. Even if it has no value.
There should be no difference between typed and none-typed properties for
this.

Greetz, Lydia

Op wo 8 feb. 2023 om 15:58 schreef Kamil Tekiela <tekiela...@gmail.com>:

> Hi Lydia,
>
> I understand where you are coming from because I encountered this a few
> times myself, but every time it was actually because I was doing something
> wrong. The reason for this limitation is actually very simple and logical.
> Let me explain.
> When you use untyped properties, the type is not restricted so every
> property can be nullable by default. It's very simple for the language to
> have NULL as the default value for all untyped properties.
> 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. Unless you assign some string value to it, the
> language will not initialize it with any value. If you need the property to
> have a state indicating no value, you can make it nullable and assign NULL
> as the default value.
>
> What you are proposing is actually not acceptable from the type system's
> point of view. We cannot add a new syntax for assigning default values to
> type properties. It might make sense in a very limited scope, e.g. public !
> int $number; defaults to 0, but in any other more complex scenario, this
> fails miserably. Consider this example:
>
> class Test {
>     public ! MyObject&SomeInterface $prop1;
>     public ! int|false|null $prop2;
> }
>
> In the example above, what default values could be assigned to the
> properties? The correct answer is none. You cannot have a default object,
> nor can you pick a default value from the union of three types. There is
> simply no way to determine the default value. So if we had a new syntax
> like this, it would be severely limited to only the simplest scenarios.
> This would create a new inconsistency for the benefit of using a single
> character instead of assigning the value. There would be no benefit to
> introducing such inconsistency to the language.
>
> It is the job of the developer to specify the default type. Some things
> can be left for PHP to infer, but when it comes to the default property
> value, the developer needs to assign it.
>
> You said that an isset is necessary when using typed properties. I would
> argue that an isset is not needed. Using isset with typed properties is a
> code smell in many circumstances. If the property can be unassigned, the
> developer should use a sentinel value such as null to indicate this. If the
> property is always expected to hold a value, then the code should not allow
> a path to access it before it's initialized.
>
> Kind Regards,
> Kamil
>

Reply via email to