Skip to content

Lazy proxy calls set hook twice #18000

Closed
Closed
@iluuu1994

Description

@iluuu1994

Description

An assignment to a backing value in the set hook of an uninitialized lazy proxy triggers triggers lazy init. After initialization, we re-read the property from the underlying object. However, since the objects are not the same, we'll actually trigger the set hook again. This is unexpected in two ways:

  • The set hook is called twice, as mentioned.
  • We'll pass the result of the first set as an input to the second set, applying any side-effects twice.

The following code:

class C {
    public $prop {
        set {
            echo "set\n";
            $this->prop = $value * 2;
        }
    }
}

$rc = new ReflectionClass(C::class);

$obj = $rc->newLazyProxy(function () {
    echo "init\n";
    return new C;
});

function foo(C $c) {
    $c->prop = 1;
    var_dump($c->prop);
}

foo($obj);

Resulted in this output:

set
init
set
int(4)

But I expected this output instead:

set
init
int(2)

PHP Version

PHP 8.4

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