Hi,

After posting this in the issues list
<https://github.com/php/php-src/issues/10537>, I was asked to mail it here.
This is my first time here, so I hope I do it the right way.

-------------------------------

At last I have the opportunity to start using php8, I am very happy with it
and how mature it has become; implementing all kinds of code quality.

But this new *uninitialized state* is really a pain.
When you set a property in a class, I consider it as set into the class; it
is part of that class, that object.
Whether it is undefined or not. You could access it with an easy if or
ternary operator.

And you got a *clean overview* of all your properties and how they are
initialized.

class Test {

  protected string $name;

  protected string $type = 'mytype';

  protected ?array $arrLog;

  protected ?string $optionalVar;

  protected string $table = 'category';

 protected bool $isOk = false;
}

*This looks clean and gives clear, easy insight* to the purpose of each
property.
But since php8, you have to use isset() or empty() to check whether a
property is initialized.

Also php is offering more and more shorthand alternatives like nullsafe
operator, ternary operator etc. Which I was not in favor of, but it seems
to be the standard nowadays, so I let phpstorm do its thing and use the
shorthands.

But when you start using the (very necessary!!!) typed properties this is
not working anymore unless you 'initialize' the property with at least an
empty or dummy value or null.
I read the RFC trying to understand.....

Maybe it is technically difficult or it is against high-standard rules
(which I love!), but this is really unpractical and gives to much overhead
and clutter.
I love good quality in coding, I endured years of being called crazy by
colleagues who learned php by google and stackoverflow and loved it because
it allowed all the bad coding. (Good is good enough)

But now it seems to go over the top, making php less practical.

For this uninitialized-thing we can choose one the following solutions at
the moment:

   - leave properties untyped, which is really bad
   - set many properties to null or other empty values, which clutters your
   property list
   - add those empty values inside the constructor, which creates useless
   extra lines of code
   - use isset or empty to check if it is set or not, and not be able to
   use shorthands

To use typed properties and profit from all the new shorthands and nice new
feature of php 8, my class has to be something like:

class Test {

  protected string $name = '';

  protected string $type = 'mytype';

  protected ?array $arrLog = [];

  protected ?string $optionalVar = null;

  protected string $table = 'category';

 protected bool $isOk = false;
}

This looks cluttered. And I do not understand that we get all those nice
shorthands, but for typed properties, we need to type more....

Why does a nullable type not automaticly default to null, or even to the
empty variant of the type (by giving it not an question-mark but the
exclamation mark, maybe...) like:

class Test {

  protected !string $name;  // defaults to ''

  protected string $type = 'mytype';

  protected !array $arrLog;  // defaults to []

  protected ?string $optionalVar;  // defaults to null

  protected string $table = 'category';

 protected !bool $isOk;  // defaults to false

 protected !float $someNumber;  // default to 0

 protected ?myObject $oObject;   // no default for objects... to
complicated and risky, or not??, so only nullable
}

I hope this can be considered again.

Greetz, flexJoly (Lydia de Jongh)
https://www.linkedin.com/in/flexjoly/
https://divaeloper.wordpress.com/

Reply via email to