[php-src] Issue #9021: After unsetting a typed object property it appears not be uninitialised, but undefined and __get is called

From: Date: Fri, 15 Jul 2022 10:07:13 +0000
Subject: [php-src] Issue #9021: After unsetting a typed object property it appears not be uninitialised, but undefined and __get is called
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-241932@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/9021
Author: frankvanhest

### Description

The following code:

```php
<?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 https://github.com/php/php-src/blob/8d84e44e7bef81b5b5d896ba1459304e94ec9b10/Zend/zend_object_handlers.c#L608.

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_


Thread (1 message)

  • frankvanhest
« previous php.bugs (#241932) next »