php-src/Zend/tests/lazy_objects/unset_hook.phpt
Arnaud Le Blanc 58aa6fc830
Lazy objects
RFC: https://wiki.php.net/rfc/lazy-objects

Closes GH-15019
2024-08-30 17:30:03 +02:00

68 lines
1.2 KiB
PHP

--TEST--
Lazy objects: cannot unset hooked property
--FILE--
<?php
class C {
public $a {
get { return $this->a; }
set($value) { $this->a = $value; }
}
public int $b = 1;
public function __construct(int $a) {
var_dump(__METHOD__);
$this->a = $a;
$this->b = 2;
}
}
function test(string $name, object $obj) {
printf("# %s:\n", $name);
var_dump($obj);
try {
unset($obj->a);
} catch (\Error $e) {
printf("%s: %s\n", $e::class, $e->getMessage());
}
var_dump($obj);
}
$reflector = new ReflectionClass(C::class);
$obj = $reflector->newLazyGhost(function ($obj) {
var_dump("initializer");
$obj->__construct(1);
});
test('Ghost', $obj);
$obj = $reflector->newLazyProxy(function ($obj) {
var_dump("initializer");
return new C(1);
});
test('Proxy', $obj);
--EXPECTF--
# Ghost:
lazy ghost object(C)#%d (0) {
["b"]=>
uninitialized(int)
}
Error: Cannot unset hooked property C::$a
lazy ghost object(C)#%d (0) {
["b"]=>
uninitialized(int)
}
# Proxy:
lazy proxy object(C)#%d (0) {
["b"]=>
uninitialized(int)
}
Error: Cannot unset hooked property C::$a
lazy proxy object(C)#%d (0) {
["b"]=>
uninitialized(int)
}