[php-src] Issue #8040: Silencing the non-existence of a local variable
| From: | dg | Date: | Sat, 05 Feb 2022 01:01:25 +0000 |
| Subject: | [php-src] Issue #8040: Silencing the non-existence of a local variable | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-239492@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8040
Author: dg
### Description
> I'm not quite sure where to open this topic? Is it better to post it here or in
> php.internals or elsewhere?
PHP has made incredible progress in the last two major versions in the strictness of error
conditions. Deprecating dynamic properties in PHP 8.2 is the next big step in this area. But I see
two things here that are unnecessarily loose and cover errors arising from oversight or typos. It
would be great if their behavior could be changed in PHP 9.
In practice, the absence of a *local variable* is almost always a logical error. But the
isset construct and the ?? operator hide them. These examples produces no
warning:
```php
// $arr doesn't exist
$foo = isset($arr[1]);
$foo = $arr[1] ?? null;
```
Both constructs are used primarily to see if the array contains the element. And if the $arr
variable doesn't exist, it's usually because of a bug in the code that I'd like to be
informed about. So I would expect a warning Undefined variable $arr.
On the other hand, and maybe unexpectedly, the unset draws attention to this:
```php
// $arr doesn't exist
unset($arr[1]); // Warning: Undefined variable $arr
```
It would be great if isset and ?? behaved the same as unset.
The same applies to objects and properties, ie. $obj->foo. Paradoxically, in this
case, unset is also silent missing variable since PHP 7.4. PHP has taken a step
backwards in this area. https://3v4l.org/BaRNF
In the case of objects, there is also ?-> operator that already behaves correctly:
```php
// $obj doesn't exist
$obj?->foo; // Warning: Undefined variable $obj
```
What do you think about this?