Skip to content

After unsetting a typed object property it appears not be uninitialised, but undefined and __get is called #9021

Closed as not planned
@frankvanhest

Description

@frankvanhest

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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions