Skip to content

Simplify initialization of ghost objects by allowing deferred constructor calls #390

Open
@Ocramius

Description

@Ocramius

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions