Skip to content

Lazy proxy calls magic methods twice #18038

Closed
@arnaud-lb

Description

@arnaud-lb

Description

Access to properties of a lazy proxy are forwarded to the real instance. But if the access is made by a magic method, forwarding to the real instance may call the magic method again:

#[AllowDynamicProperties]
class C {
    public $_;
    public function __set($name, $value)
    {
        var_dump(__METHOD__);
        $this->$name = $value * 2;
    }
}

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

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

$obj->prop = 1;
var_dump($obj->prop);

Resulted in this output:

string(8) "C::__set"
init
string(8) "C::__set"
int(4)

But I expected this output instead:

string(8) "C::__set"
init
int(2)

PHP Version

PHP 8.4

Operating System

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions