3
votes

We are currently writing a module in Zend Framework 2.

I am having quite some trouble finding documentation on this, but know it is possible.

I have 3 classes in a hierarchy that I would like to setup via dependency injection. Let's call these classes; ClassA, ClassB and ClassC.

ClassA contains an array of ClassB instances and ClassB contains an array of ClassC instances. Each instance of ClassB should be instantiated using different parameters. The same applies for ClassC. Parameters for all 3 classes are passed in via the constructor (this can also be handled with a setter if needed.).

<?php
class ClassA {
    protected $arrClassBInstances = array();

    public function __construct( $arrClassBInstances ) {
         $this->arrClassBInstances = $arrClassBInstances;
    }
}

class ClassB {
    protected $arrClassCInstances = array();
    protected $someOtherParam = "";

    public function __construct( $arrClassCInstances, $someOtherParam ) {
         $this->arrClassBInstances = $arrClassCInstances;
         $this->someOtherParam = $someOtherParam;

    }
}

class ClassC {
    protected $someParam = "";

    public function __construct( $someParam ) {
         $this->someParam = $someParam;
    }
}

So their are 2 parts to my questions. First, How can I configure multiple instances of the same class with different parameters injected? For Example, multiple instances of ClassB each with it's own subset of ClassC instances. There has to be some kind of alias in the ZF2 DiC.

Secondly how can I inject an array of reconfigure dependencies into a class. For Example, How can I inject an array of ClassB instances into ClassA?

If possible please provide me with an example using the DiC directly and an example of achieving this in the DI section of the module.config.php.

3

3 Answers

0
votes

From the documentation, i think this should works:

// $event instance of \Zend\Mvc\MvcEvent
$di = $event->getTarget()->getLocator();
$paramsForA = array(
    $di->get('qualified_namespaces_or_di_alias_for_b', array('arrClassCInstances'=>array(
        $di->get('qualified_namespaces_or_di_alias_for_c', array('someParam'=>1)),
        $di->get('qualified_namespaces_or_di_alias_for_c', array('someParam'=>2)),
    ))),
    $di->get('qualified_namespaces_or_di_alias_for_b', array('arrClassCInstances'=>array(
        $di->get('qualified_namespaces_or_di_alias_for_c', array('someParam'=>3)),
        $di->get('qualified_namespaces_or_di_alias_for_c', array('someParam'=>4)),
    ))),
);
$classA = $di->get('qualified_namespaces_or_di_alias_for_a', array('arrClassBInstances'=>$paramsForA));

You Either use DI alias or full qualified namespace as the first parameters of $di->get()

0
votes

I just wrote a post on this subject. I'm personally not using the Di class, or DiC, but hopefully you will find that the concepts are can be directly applied to your question, especially in part 2, regardless of what you choose to use as your IoCC. http://zendblog.shinymayhem.com/2012/09/using-servicemanager-as-inversion-of.html