-
Notifications
You must be signed in to change notification settings - Fork 8k
After unsetting a typed object property it appears not be uninitialised, but undefined and __get is called #9021
Copy link
Copy link
Closed as not planned
Closed as not planned
Copy link
Labels
Description
Description
The following code:
<?php
declare(strict_types=1);
final class Foo
{
private string $string;
public function __get(string $propertyName): void
{
throw new \Exception('Class property which was unset earlier is now passed to __get');
}
public function setString(string $string): void
{
$this->string = $string;
}
public function string(): string
{
return $this->string;
}
public function unsetString(): void
{
unset($this->string);
}
}
$foo = new Foo();
try {
echo $foo->string() . PHP_EOL;
} catch (\Throwable $throwable) {
echo $throwable->getMessage() . PHP_EOL;
}
$foo->setString('test');
echo $foo->string() . PHP_EOL;
$foo->unsetString();
try {
echo $foo->string() . PHP_EOL;
} catch (\Throwable $throwable) {
echo $throwable->getMessage() . PHP_EOL;
}Resulted in this output:
Typed property Foo::$string must not be accessed before initialization
test
Class property which was unset earlier is now passed to __get
But I expected this output instead:
Typed property Foo::$string must not be accessed before initialization
test
Typed property Foo::$string must not be accessed before initialization
There was an issue of this way back, https://bugs.php.net/bug.php?id=78904 and the changes are still present in de source code
php-src/Zend/zend_object_handlers.c
Line 608 in 8d84e44
| if (UNEXPECTED(Z_PROP_FLAG_P(retval) == IS_PROP_UNINIT)) { |
Not sure if this is a bug, or my interpretation of the behaviour for unsetting a typed object property is wrong.
Thank you in advance for looking at this
PHP Version
Latest of PHP 7.4, PHP 8.0, PHP 8.1
Operating System
No response
Reactions are currently unavailable