Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lyrixx opened this issue May 2, 2025 · 6 comments
Closed

Comments

@lyrixx
Copy link
Member

lyrixx commented May 2, 2025

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

@nicolas-grekas
Copy link
Member

What the error says: this is not possible, unless you use PHP 8.4

@nicolas-grekas
Copy link
Member

Using 8.4 with native lazy ghosts of course!

@nicolas-grekas nicolas-grekas closed this as not planned Won't fix, can't repro, duplicate, stale May 2, 2025
@lyrixx
Copy link
Member Author

lyrixx commented May 2, 2025

I use php 8.4. what did I miss?
I mean, how can I fix that?

@nicolas-grekas
Copy link
Member

You have to use native lazy objects.

@lyrixx
Copy link
Member Author

lyrixx commented May 3, 2025

Actually, the issue is in doctrine. I made a very simple reproducer, but I have such code in my entity.

I fail to see why it works without hook, but it does not with. More over, I have not set hook, so VarExporter could set one that throw an exception for example

@nicolas-grekas
Copy link
Member

That's how hooks have been designed. You have to enable native lazy objects for Doctrine (not sure it's been released yet)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants