ZF2-260: Support for classnames as parameter value in DI

Issue Type: New Feature Created: 2012-04-11T14:52:13.000+0000 Last Updated: 2012-06-25T21:27:08.000+0000 Status: Resolved Fix version(s): Reporter: Stefan Kleff (dershao) Assignee: Ralph Schindler (ralph) Tags: - Zend\Di

  • ClassName
  • DI
  • Doctrine
  • String

Related issues: Attachments:


Currently I'm trying to configure Doctrine2 with ZF2/DI and in some cases it's necessary to specify a class name (string). Example: "defaultRepositoryClassName" parameter of Doctrine\ORM\Configuration should be something like "MyLib\MyRepository" This is (afaik) not possible with the current DIC because "MyLib\MyRepository" will be recognized as class and therefore it will we instantiated and a reference will be used during injection.


Posted by Stefan Kleff (dershao) on 2012-04-11T15:14:48.000+0000

<pre class="highlight">
return array(
    'di' => array(
        'definition' => array(
            'class' => array(
                'Doctrine\ORM\Configuration' => array(
                    'methods' => array(
                        'setDefaultRepositoryClassName' => array(
                                'defaultRepositoryClassName' => array('type' => false, 'required' => true)

Posted by Stefan Kleff (dershao) on 2012-04-11T15:15:20.000+0000

'type' => false does the magic ;)

Posted by Stefan Kleff (dershao) on 2012-04-18T09:47:58.000+0000

Assuming that type => false is the correct way to inject strings instead of classnames, I found another part where this is currently not working:

Use case: There is a class with a method which expects a classname (not an instance). You want to inject multiple class names in this method

Given: definition with method "addMyParam" and a param "myClassName" => array('type'=>false). instance with 'injections' array to set the classnames.

Result: Somehow the class definition of the given class names is already known by DI. Maybe because of some RunetimeDefinition (I just started looking into DI). During resolveMethodParameters() in the "priority 1"-code the given string (classname) will be checked if the string is a known class name (which is the case) and instantiated.

Expected: Still assuming that type=>false indicates that it is a string instead of an object, it should not instntiate the class.

Bugfix: Checking if the classname is known by the DI AND checking if type !== false

Posted by Ralph Schindler (ralph) on 2012-06-25T21:27:08.000+0000

Fixed in master

Have you found an issue?

See the Overview section for more details.


© 2006-2017 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.