[php-src] Issue #10537: 'Uninitialized' creates a lot of cluttering
| From: | flexjoly | Date: | Tue, 07 Feb 2023 19:04:33 +0000 |
| Subject: | [php-src] Issue #10537: 'Uninitialized' creates a lot of cluttering | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-243667@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/10537
Author: flexjoly
### Description
Hi,
I hope I am allowed to write this here. I cannot make a real RFC or so. So I hope this is ok.
At last I have the opportunity to start using php8, I am very happy 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.
```php
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:
```php
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 (for example) like:
```php
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, so only
nullable
}
```
I hope this can be considered again.
Thanks in advance, flexJoly