Skip to content

[VarExporter] Cannot generate lazy ghost: property XXXX is final or private(set) #60333

Closed as not planned
@lyrixx

Description

@lyrixx

Symfony version(s) affected

7.3 (maybe older too)

Description

I'm playing with property hook from PHP 8.4, and VarExporter component is not able to process my code.

I use a backed property, with a private(set) modifier, and a hook. This blocks the VarExporter to generate a ghost object.
I'm not sure how I could solve this, except by changing the visibility. And I fail to see why the hook change everything 🤔

How to reproduce

require __DIR__ . '/vendor/autoload.php';

use Symfony\Component\VarExporter\ProxyHelper;

function prepare(string $foo): string
{
    echo "calling prepare\n";

    return 'prepared ' . $foo;
}

class Foobar
{
    public function __construct(
        public private(set) string $foo,
    ) {
    }

    public private(set) string $fooPrepared {
        get => $this->fooPrepared ??= prepare($this->foo);
    }

    // this version works
    // public private(set) string $fooPrepared = 'prepared foo';
}

$foobar = new Foobar('foo');
dump($foobar->fooPrepared);
dump($foobar->fooPrepared);

$code = ProxyHelper::generateLazyGhost(new ReflectionClass(Foobar::class));
echo $code;

Possible Solution

No response

Additional Context

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