[php-src] Issue #18766: Nested lazy ghost fails with clone
| From: | mkroeders | Date: | Wed, 04 Jun 2025 20:08:38 +0000 |
| Subject: | [php-src] Issue #18766: Nested lazy ghost fails with clone | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-250206@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/18766
Author: mkroeders
### Description
Description:
In a library I do reflection of objects and want to support nested references. So use lazy
ghost/proxies for creating nested objects to prevent endless loop and with a combination of caching
to prevent redoing the same objects multiple times. However it breaks when using clone inside the
factory method. Tested it with the
newLazyGhost and newLazyProxy.
Not sure if it is supported behavior, but looking at the
[rfc](https://wiki.php.net/rfc/lazy-objects) it only mentions nesting in combination with throwing
exceptions.
The following code:
```php
<?php
class Bar {
public function __construct(
public Bar $bar,
public int $x,
) {}
public function with(): self
{
return clone $this;
}
}
class Fiz {
public mixed $cache = null;
public function foo(int $i)
{
if (!$this->cache) {
$this->cache = $this->factor($i);
}
if ($i <= 1) {
return $this->cache->with();
}
return $this->cache;
}
public function factor(int $i): object
{
$reflector = new ReflectionClass(Bar::class);
return $reflector->newLazyGhost(
function (Bar $bar) use ($i) {
$bar->__construct($this->foo($i + 1), 2);
},
);
}
}
$fiz = new Fiz();
$f = $fiz->foo(0);
var_dump($f->bar->bar->x);
```
https://3v4l.org/lYpu1
Resulted in this output:
```
PHP Fatal error: Uncaught Error: Typed property Bar::$bar must not be accessed before
initialization
```
But I expected this output instead:
```
int(2)
```
### PHP Version
```plain
PHP 8.4.3 (cli) (built: Jan 15 2025 01:03:17) (NTS)
Copyright (c) The PHP Group
Built by Homebrew
Zend Engine v4.4.3, Copyright (c) Zend Technologies
with Xdebug v3.4.0, Copyright (c) 2002-2024, by Derick Rethans
with Zend OPcache v8.4.3, Copyright (c), by Zend Technologies
```
### Operating System
Mac OS