Open
Description
As it currently stands, the deferred initialization of a ghost object (as per documentation) looks quite messy:
$initializer = function (
\ProxyManager\Proxy\GhostObjectInterface $ghostObject,
string $method,
array $parameters,
& $initializer,
array $properties
) {
$initializer = null; // disable initializer for this proxy instance
// initialize properties (please read further on)
$properties["\0ClassName\0foo"] = 'foo';
$properties["\0ClassName\0bar"] = 'bar';
return true; // report success
};
Considering that we now support original constructors, a simpler solution could be as following:
$initializer = function (
\ProxyManager\Proxy\GhostObjectInterface $ghostObject,
string $method,
array $parameters,
& $initializer,
array $properties
) {
$initializer = null; // disable initializer for this proxy instance
// less efficient, but simpler and more portable:
$ghostObject->__construct('foo');
$ghostObject->setBar('bar');
return true; // report success
};
This is a bit weird to read, but much simpler, and it would make using ghost objects in the symfony dependency injection container much simpler.
Whether this needs any code change at all (shouldn't) is still to be verified via integration tests, but this logic should already work as per 2.1.x
.
/cc @nicolas-grekas @Danack this is about today's discussion about laziness.