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

Add check to DI to see if we have a class to instantiate #4790

Closed

Conversation

nickpeirson
Copy link
Contributor

Currently the DI container throws a fatal error when trying to instantiate a class that doesn't exist or, more usually, when DI tries to instantiate an interface.

The fatal error looks like:

PHP Fatal error:  Cannot instantiate interface Foo\Bar in /Users/nickpeirson/projects/mycg/vendor/zendframework/zend-di/Zend/Di/Di.php on line 452

This PR changes the behaviour to check for the existence of the of the class before trying to instantiate it and throws and exception accordingly. This results in a much more developer friendly backtrace allowing a developer to identify the problem more quickly and spend less time debugging. As we see this most commonly when we've missed a type preference for an interface I've also added a case for when the class to be instantiated is in fact an interface, to add clarity to the thrown exception.

Also added a special case for when we're mistakenly trying to instantiate an interface
@samsonasik
Copy link
Contributor

please provide unit test.

@nickpeirson
Copy link
Contributor Author

Add a unit test that covers the exception being thrown. Working through it it seems like the underlying problem could be with the definition produced by Zend\Di\Definition\CompilerDefinition, as I can't reproduce it just using the runtime definition.

That being said, with the current state of the definition compiler this is a problem (I don't have time right now to dig into the compiler, but I may come back to it in the future), however this mitigates it so I feel it's worth adding.

* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Di
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove @Package

weierophinney added a commit that referenced this pull request Jul 19, 2013
Add check to DI to see if we have a class to instantiate
weierophinney added a commit that referenced this pull request Jul 19, 2013
- Use sprint for exception message string interpolation
- No need for escaping namespace separators when class names are
  uppercase
weierophinney added a commit that referenced this pull request Jul 19, 2013
@ghost ghost assigned weierophinney Jul 19, 2013
weierophinney added a commit to zendframework/zend-di that referenced this pull request May 15, 2015
…DIClassExistenceCheck

Add check to DI to see if we have a class to instantiate
weierophinney added a commit to zendframework/zend-di that referenced this pull request May 15, 2015
- Use sprint for exception message string interpolation
- No need for escaping namespace separators when class names are
  uppercase
weierophinney added a commit to zendframework/zend-di that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-di that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants