Skip to content

missing isset() on private(set) property (backing a get hook) #18318

Open
@hakre

Description

@hakre

Description

i'm able to trigger an infinite recursion by using isset() on a private(set) property that has a get hook:

<?php

// https://github.com/php/php-src/issues/18318

$foo = new CatchMeIfYouCan();

var_dump($foo->bar);

final class CatchMeIfYouCan
{
    private(set) int $bar
    {
        get => $this->bar ??= $this->fooBar();
    }

    public function fooBar(): int
    {
        isset($this->bar);
    }
}

https://3v4l.org/LALTu

this came unexpected to me as it is normally possible to make use of isset() or ??= within a class safely.

expected is to fail as fooBar() does not return an integer.


and it seems to me that the use of ??= on the member is only possible within the hook, but not within a different place in the class's protocol.

example:

    ...

    public function fooBar(): int
    {
        return $this->bar ??= 1;
    }
}

PHP Version

PHP 8.4.5 (cli) & php-src/sapi/cli/php

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