Skip to content

Commit 25672ed

Browse files
committed
Fix GH-21478: Forward property operations to real instance for initialized lazy proxies
zend_std_read_property, write_property, unset_property, and has_property invoked magic methods on the proxy even when the real instance's guard was already set, causing double invocations. Forward directly to the real instance when its guard indicates a recursive call. Updates gh18038-002 and gh18038-009 test expectations to match.
1 parent d34c840 commit 25672ed

File tree

11 files changed

+214
-12
lines changed

11 files changed

+214
-12
lines changed

Zend/tests/lazy_objects/gh18038-002.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,4 @@ var_dump($obj->prop);
3434
--EXPECT--
3535
init
3636
string(19) "RealInstance::__set"
37-
string(12) "Proxy::__set"
3837
int(2)

Zend/tests/lazy_objects/gh18038-004.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ var_dump($real->prop);
3636
--EXPECTF--
3737
init
3838
string(19) "RealInstance::__get"
39-
string(12) "Proxy::__get"
4039

4140
Warning: Undefined property: RealInstance::$prop in %s on line %d
4241
NULL

Zend/tests/lazy_objects/gh18038-007.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,5 @@ var_dump(isset($real->prop['']));
3636
--EXPECT--
3737
init
3838
string(21) "RealInstance::__isset"
39-
string(14) "Proxy::__isset"
4039
bool(false)
4140
bool(false)

Zend/tests/lazy_objects/gh18038-009.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,5 @@ var_dump(isset($real->prop));
3636
--EXPECT--
3737
init
3838
string(21) "RealInstance::__isset"
39-
string(14) "Proxy::__isset"
4039
bool(false)
4140
bool(false)

Zend/tests/lazy_objects/gh20875.phpt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ Warning: Undefined variable $a in %s on line %d
3131

3232
Warning: Undefined variable $v in %s on line %d
3333

34-
Notice: Indirect modification of overloaded property A::$b has no effect in %s on line %d
35-
36-
Warning: Undefined variable $x in %s on line %d
37-
38-
Notice: Object of class stdClass could not be converted to int in %s on line %d
39-
40-
Warning: Undefined variable $v in %s on line %d
41-
4234
Notice: Indirect modification of overloaded property A::$f has no effect in %s on line %d
4335

4436
Fatal error: Uncaught Error: Cannot assign by reference to overloaded object in %s:%d
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-21478: __isset on lazy proxy should not double-invoke when real instance guard is set
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public $_;
8+
9+
public function __isset($name) {
10+
global $proxy;
11+
printf("__isset(\$%s) on %s\n", $name, $this::class);
12+
return isset($proxy->{$name});
13+
}
14+
}
15+
16+
class Bar extends Foo {}
17+
18+
$rc = new ReflectionClass(Bar::class);
19+
$proxy = $rc->newLazyProxy(function () {
20+
echo "Init\n";
21+
return new Foo();
22+
});
23+
24+
$real = $rc->initializeLazyObject($proxy);
25+
isset($real->x);
26+
27+
?>
28+
--EXPECT--
29+
Init
30+
__isset($x) on Foo
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-21478: Proxy's own __get runs when accessed directly (not from real instance)
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
private $_;
8+
9+
public function __get($name) {
10+
echo __CLASS__, " ", $name, "\n";
11+
}
12+
}
13+
14+
class Bar extends Foo {
15+
public function __get($name) {
16+
echo __CLASS__, " ", $name, "\n";
17+
}
18+
}
19+
20+
$rc = new ReflectionClass(Bar::class);
21+
$proxy = $rc->newLazyProxy(function () {
22+
return new Foo();
23+
});
24+
$rc->initializeLazyObject($proxy);
25+
26+
$proxy->x;
27+
28+
?>
29+
--EXPECT--
30+
Bar x
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
GH-21478: __set on lazy proxy should not double-invoke when real instance guard is set
3+
--FILE--
4+
<?php
5+
6+
#[AllowDynamicProperties]
7+
class Foo {
8+
public $_;
9+
10+
public function __set($name, $value) {
11+
global $proxy;
12+
printf("__set(\$%s) on %s\n", $name, $this::class);
13+
$proxy->{$name} = $value;
14+
}
15+
}
16+
17+
#[AllowDynamicProperties]
18+
class Bar extends Foo {}
19+
20+
$rc = new ReflectionClass(Bar::class);
21+
$proxy = $rc->newLazyProxy(function () {
22+
echo "Init\n";
23+
return new Foo();
24+
});
25+
26+
$real = $rc->initializeLazyObject($proxy);
27+
$real->x = 1;
28+
29+
?>
30+
--EXPECT--
31+
Init
32+
__set($x) on Foo
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-21478: __unset on lazy proxy should not double-invoke when real instance guard is set
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public $_;
8+
9+
public function __unset($name) {
10+
global $proxy;
11+
printf("__unset(\$%s) on %s\n", $name, $this::class);
12+
unset($proxy->{$name});
13+
}
14+
}
15+
16+
class Bar extends Foo {}
17+
18+
$rc = new ReflectionClass(Bar::class);
19+
$proxy = $rc->newLazyProxy(function () {
20+
echo "Init\n";
21+
return new Foo();
22+
});
23+
24+
$real = $rc->initializeLazyObject($proxy);
25+
unset($real->x);
26+
27+
?>
28+
--EXPECT--
29+
Init
30+
__unset($x) on Foo
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
GH-21478 (Property access on lazy proxy may invoke magic method despite real instance guards)
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public $_;
8+
9+
public function __get($name) {
10+
global $proxy;
11+
printf("__get(\$%s) on %s\n", $name, $this::class);
12+
return $proxy->{$name};
13+
}
14+
}
15+
16+
class Bar extends Foo {}
17+
18+
$rc = new ReflectionClass(Bar::class);
19+
$proxy = $rc->newLazyProxy(function () {
20+
echo "Init\n";
21+
return new Foo();
22+
});
23+
24+
$real = $rc->initializeLazyObject($proxy);
25+
$real->x;
26+
27+
?>
28+
--EXPECTF--
29+
Init
30+
__get($x) on Foo
31+
32+
Warning: Undefined property: Foo::$x in %s on line %d

0 commit comments

Comments
 (0)