Skip to content

Document limitations when using lazy loaded dependencies that involve variadic functions #359

@holtkamp

Description

@holtkamp

When using lazy loaded dependencies, I encountered some curious behaviour when handing over more parameters to a function than defined in its signature (variadic functions).

class Service
{
    /**
     * @param mixed $param1
     * @param mixed $param2
     * @param mixed $paramN Can be used to supply additional parameters
     */
    public function doStuf($param1, $param2){
        echo func_num_args();
    }
}

Now let's try to use this Service when fetched from the dependency injection container

$dic->get('Service')->doStuff(1, 2, 3); //Prints '3' when not lazy loaded
$dic->get('Service')->doStuff(1, 2, 3); //Prints '2' when lazy loaded

I use this approach to inject extra variables when translating messages like: "User X has logged in on Y", where X and Y would be 'additional' parameters.

This probably has to do with the intermediate proxy classes that are generated. For example from a generated proxy class:

  /**
     * {@inheritDoc}
     */
    public function doStuf($param1, $param2)
    {
        $this->initializer5683c4a86687a054344119 && $this->initializer5683c4a86687a054344119->__invoke($this->valueHolder5683c4a866860700867865, $this, 'fireEventOnOrder', array('param1' => $param1, 'param2' => $param2), $this->initializer5683c4a86687a054344119);

        return $this->valueHolder5683c4a866860700867865->fireEventOnOrder($param1, $param2);
    }

When looking at Ocramius/ProxyManager#177, I think this behaviour is expected. Therefore I think it should be mentioned in the documentation as a warning...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions