[php-src] Issue #8179: Typed properties: the unset() command mahe property undeclared instead of uninitialized
| From: | jakubboucek | Date: | Mon, 07 Mar 2022 10:33:36 +0000 |
| Subject: | [php-src] Issue #8179: Typed properties: the unset() command mahe property undeclared instead of uninitialized | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-240245@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8179
Author: jakubboucek
### Description
The following code:
```php
<?php
class Dummy {
private string $prop;
public function __set($name, $value) {
echo "Property '$name' in undeclared!\n";
$this->$name = $value;
}
}
$dummy = new Dummy;
// This write to uninitialized property - nothing printed
$dummy->prop = 'First attempt';
echo "{$dummy->prop}\n";
unset($dummy->prop);
// This write to **undeclared** property - message is printed
$dummy->prop = 'Second attempt';
echo "{$dummy->prop}\n";
```
Resulted in this output:
```
First attempt
Property 'prop' in undeclared!
Second attempt
```
Live example: https://3v4l.org/ZnWbm
But I expected this output instead:
```
First attempt
Second attempt
```
The [PHP
RFC](https://wiki.php.net/rfc/typed_properties_v2#:~:text=If%20a%20typed%20property%20is%20unset()%2C%20then%20it%20returns%20to%20the%20uninitialized%20state.)
is explicitly say:
>If a typed property is
unset(), then it returns to the
>uninitialized state.
But it's not true, because the unset() command doesn't returns to the
uninitialized state but it's just undeclare them and the uninitialized state is simulated at
runtime when somebody is trying to read it.
The different is evident when I trying write to property – property in uninitialized state does
not call magic __set() method, undeclared property does.
### PHP Version
PHP 7.4.0 - 7.4.28, 8.0.1 - 8.0.16, 8.1.0 - 8.1.3
### Operating System
_No response_