Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Zend DI Factory Method called twice #2470

Closed
zfbot opened this issue Sep 28, 2012 · 5 comments
Closed

Zend DI Factory Method called twice #2470

zfbot opened this issue Sep 28, 2012 · 5 comments
Labels

Comments

@zfbot
Copy link

zfbot commented Sep 28, 2012

Jira Information

Original Issue:ZF2-261
Issue Type:Bug
Reporter:stefankleff
Created:04/12/12
Assignee:ralphschindler
Components:Zend\Di

Description

When using a factory method with a parameter definition to create a class the factory method will be called twice:

Example:

return array(
    'di' => array(
        'definition' => array(
            'class' => array(
                'MyClass' => array(
                    'instantiator' => array('MyClass', 'create'),
                    'methods' => array(
                        'create' => array(
                            'param'       => array('type' => 'MyOtherClass', 'required' => true),
                        ),
                    ),
                ),
            ),
        ),
    ),
 );

MyClass will be created during Di::newInstance by Di::createInstanceViaCallback().
If the instantiator method is "__construct" the "__construct"-method will be removed from the array of possible injection methods ($injectionMethods). If the instantiator is something different, the method (in the example "create") will NOT be removed. So the method is called again from Di::handleInjectionMethodForInstance() which results in a second instance of MyClass.

@zfbot
Copy link
Author

zfbot commented Sep 28, 2012

(Originally posted by: ralphschindler on 06/25/12)

I am closing the related pull request, with the information above, this is hard to follow your use case, can you provide more information and/or a unit test?

@zfbot
Copy link
Author

zfbot commented Sep 28, 2012

This issue was ported from the ZF2 Jira Issue Tracker at
http://framework.zend.com/issues/browse/ZF2-261

Known GitHub users mentioned in the original message or comment:
@stefankleff, @ralphschindler

@Ocramius
Copy link
Member

@stefankleff still valid also after #3008 ?

@stefankleff
Copy link
Contributor

Honestly I'm not using DI anymore since ZF2 switched to the ServiceLocator. But it looks like your PR prevents that the static factory method is added to the injectionMethods array. (I assume that getMethods() will not return any static functions anymore.)

$injectionMethods[$class] = $definitions->getMethods($class);

Therefore my issue is solved!

@Ocramius
Copy link
Member

@stefankleff awesome :)

@Maks3w Maks3w closed this as completed Nov 19, 2012
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants