Dependency Injection in PHP
Join the DZone community and get the full member experience.
Join For FreeIf you have worked with frameworks such as Spring or Google Guice, you will understand the power and flexibility that dependency injection provides your humongous code base. Recently, I have seen many projects with lots of complicated components that suffer form the following syndrome:
Lack of DI:
<?php class MyService { private $_dao; private $_reportingService; private $_emailService; private $_userService; .... all the transitive dependent classes private $_dbEngine; ... function __construct($dao = null, $reportingService = null, $emailService = null, $userService = null) { if(!$dao) { $this->_dbEngine = new DBEngine(); $dao = new DAO($this->_dbEngine); } if(!$reportingService) { $reportingService = new ReportingService(); } if(!$emailService) { $emailService = new EmailService(); } ...... Continue initializing ..... Ugh! $this->_dao = $dao; $this->_reportingService = $reportingService; ....... and on and on ... } } ?>
Putting aside the principles that classes should do 1 thing and be
highly cohesive, oftentimes you will need to write classes that talk to
many components at once. So back to our problem...
If the code above isn't bad enough, imagen that the DAO, ReportingService, and/or EmailService each had their own long list of constructor arguments.
In this case, we needed to know about the underlying DB technology used
in the DAO layer. Now your service class must be aware of the all of
the transitive dependencies...Ugh!.
To alleviate the pain from instantiating this class, we make use of
PHP's awesome default value feature so that each constructor argument
accepts NULL as a default value. Now since the first one accepts a NULL
value, all others must specify the same behavior. So, this allows you to
instantiate your service as:
$myService-> = new MyService();
There are many people writing about coding best practices that have come
up with magical numbers for how many constructor arguments a class
should have. Some people say 3 others 5 -- I don't particularly have a
number in mind, so long as your classes read "nicely".
Other people argue: Instead of constructor arguments, you should have
your dependencies applied using setter functions. Certainly that
minimizes the initialization code, but now your users must remember to
set all of the dependencies when instantiating your class.
So, to put an end to this I am examining the possibility of creating
(yet another) a dependency injection framework that will manage all of
my services, components (and their dependencies) for PHP. I've
researched many frameworks hosted in Github and read how other people
are trying to solve this problem. I haven't been impressed with any of
them.
One quick drawback that I see is that the stateless nature of PHP makes
things like object containers not very practical. On the other hand,
Java object containers live in memory and they essentially give you a
registry of singletons (also called beans) that you can load and use
without having to explicitly initialize them. In PHP, this becomes a
challenge as you must rebuild the object container at every request. For
small dependency graphs, this is probably not such a big deal, but for
very deep object graphs you can start seeing performance problems.
In addition, I would like to make it as elegant as the Java frameworks
which take advantage of things like annotations and reflection. This
makes classes a lot more readable and compact. To the user, the class
above can look like the following:
Now With DI
<?php class MyService { private $_dao; private $_reportingService; private $_emailService; private $_userService; /** * Build a new service * @Inject(class=DAO) * @Inject(class=ReportingService) * @Inject(class=EmailService) * @Inject(class=UserService) */ function __construct($dao, $reportingService, $emailService, $userService) { $this->_dao = $dao; $this->_reportingService = $reportingService; $this->_emailService = $emailService; ... } } ?>
This looks a lot cleaner and notice that now my class does not need to
know about the DAO's dependencies, it does not need to know about
DBEngine. These will be managed by the object container or application
context.
Ideally, this dependency injection framework should borrow some of the
vocabulary from Spring and Java's JSR-330 annotations for DI. You can
use things like:
- @Inject
- @Named
- @Qualifier
- @Scope
- @Singleton
- Provider
Of particular, interest to me are @Inject and Providers. With Providers
you can delegate the instantiation of a particular class to your own
custom implementation. Many things come to mind with this idea. Since I
am overloading comments in PHP, I don't how fast I can scan the comment
strings for annotations. I have seen solutions where they read the PHP
class file completely and scan it instead of using PHP reflection and
things like $constructor->getDocComment(). My biggest worry however
is: will this work with applications using opcode caching such as APC? I
read that eAccelerator can strip away comments. There's a high
possibility that APC will do the same. If you have any ideas, please
chime in with a comment, and we can discuss this further.
With DI in place, instantiating a bean becomes:
$myService = BeanFactory::forName('MyService');
The first time this class is requested, it will build its dependency
graph on the fly and place it and all of its dependencies in the
application context for later use. We can make this process quicker and
provide an initialization file (YAML, XML, or JSON) or a PHP map that
maps all of the beans to their respective names. This is done to save
time on the text parsing. Many solutions out there support this in some
way, so it makes that I will support it as well.
I am still laying out all of the pieces, but I will post the main routine to look for some feedback:
public static function forName($classname, $depth = 1) { if(!class_exists($classname)) { // Either class does not exist (misspelled) or class // definition has not been included throw new \InvalidArgumentException('Class for name ['. $classname .'] does not exist. '. 'Check class name and make sure class definition is included'); } if(self::$BEAN_DEPTH == $depth) { // Don't even bother to do a look up, just instantiate without // container return util\DIUtils::instantiate($classname); } $CONTEXT = ApplicationContext::get(); $beanDef = null; $ns = '\\'; $name = $classname; if(strpos($classname, '\\') !== false) { // check namespace defined with classname $classdef = split('\\', $classname); $ns = $classdef[0]; $name = $classdef[1]; } $beanDef = $CONTEXT->getBeanDefinition($name, $ns); if(!$beanDef) { $beanDef = BeanBuilder::build( array( 'classname' => $name, 'ns' => $ns )); $CONTEXT->addBeanDefinition($beanDef); } return $beanDef->instance; }
And the BeanBuilder::build function as follows:
static function build(array $def) { $classname = $def ['classname']; $ns = $def ['ns']; $clazz = new \ReflectionClass ( $ns . $classname ); if (!$clazz->isInstantiable ()) { throw new error\BeanInitializationException ( $classname ); } // Instantiate class // In PHP 5.4 I could use, $newInstance = $clazz->newInstanceWithoutConstructor (); $annotations = array (); // Look for constructor annotations $constructor = $clazz->getConstructor(); $constructorArgs = array(); if($constructor) { // Provide simple instantiations of parameters by default // If constructor declaration has no type hinting, I cannot infer // a default instantiation for that parameter, it should stay NULL in that case foreach($constructor->getParameters() as $param) { try { $c = $param->getClass(); if($c && !$c->isInterface()) { $paramname = $c->getName(); $constructorArgs[$paramname] = new $paramname; } } catch(\ReflectionException $re) { // ignore and leave parameter init as null } } // Collect dependent instances for constructor $beans = array(); // Parse constructor annotation and look for any injection clauses foreach (parser\AnnotationParser::parse ($constructor->getDocComment()) as $a) { $a->addOption('method', model\InjectMethod::CONSTRUCTOR); $annotations [] = $a; // Recursively lookup/build dependencies $beanname = $a->getOptionSingleValue("class"); // Override simple instances with beans $constructorArgs[$beanname] = BeanFactory::forName($beanname); } } $newInstance = util\DIUtils::instantiate($clazz->getName(), $constructorArgs); $beanDef = new model\BeanDefinition (); $beanDef->ns = $clazz->getNamespaceName(); $beanDef->classname = $classname; $beanDef->annotations = $annotations; $beanDef->instance = $newInstance; return $beanDef; }
Please share your thoughts and comments. Is this feasible to do in PHP, or is there a better approach?
Published at DZone with permission of Luis Atencio, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments