- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 17
Open
Description
I have an issue when trying to read public readonly properties from the Proxy. I got an error:
Cannot initialize readonly property App\Entity\Customer::$firstName from scope App\Command\Command
Library version: 1.0.12
Preconditions:
We have a class only with public properties.
<?php
declare(strict_types=1);
namespace App\Entity;
class Customer
{
    public function __construct(
        public readonly string $firstName,
        public readonly string $lastName,
    ) {
    }
}Steps to Reproduce:
- Create a Proxy object
$instance = (new LazyLoadingGhostFactory())->createProxy(
    Customer::class,
    static function (
        GhostObjectInterface $ghostObject,
        string $method,
        array $parameters,
        ?Closure &$initializer,
        array $properties,
    ) {
        $initializer = null;
        $properties['firstName'] = 'User';
        $properties['lastName'] = 'Test';
        return true;
    },
);- Getting value from any public property results in an error.
 Cannot initialize readonly property App\Entity\Customer::$firstName from scope App\Command\Command
$output->writeln('First Name: ' . $customer->firstName);
$output->writeln('Last Name: ' . $customer->lastName);After investigation, I see the $class and $scopeObject variables equal to the App\Command\Command.
} elseif (isset(self::$privateProperties98259[$name])) {
    $callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
    $caller  = isset($callers[1]) ? $callers[1] : [];
    $class   = isset($caller['class']) ? $caller['class'] : '';
    ...
}
...
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
$accessor = function & () use ($targetObject, $name) {
    return $targetObject->$name;
};
$backtrace = debug_backtrace(true, 2);
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();Note
But if we will add a getter for the property all works without errors.
class Customer
{
    public function __construct(
        public readonly string $firstName,
        public readonly string $lastName,
    ) {
    }
    public function getFirstName(): string
    {
        return $this->firstName;
    }
}$output->writeln('First Name: ' . $customer->getFirstName());
$output->writeln('Last Name: ' . $customer->lastName);Metadata
Metadata
Assignees
Labels
No labels