views:

234

answers:

3

Hello everyone. I'm starting a new project and setting up the base to work on. A few questions have risen and I'll probably be asking quite a few in here, hopefully I'll find some answers.

First step is to handle dependencies for objects. I've decided to go with the dependency injection design pattern, to which I'm somewhat new, to handle all of this for the application.

When actually coding it I came across a problem. If a class has multiple dependencies and you want to pass on multiple dependencies via the constructor (so that they cannot be changed after you instantiate the object).

How do you do it without passing an array of dependencies, using call_user_func_array(), eval() or Reflection? This is what i'm looking for:

<?php

class DI
{
    public function getClass($classname)
    {
        if(!$this->pool[$classname]) {
            # Load dependencies
            $deps = $this->loadDependencies($classname);

            # Here is where the magic should happen
            $instance = new $classname($dep1, $dep2, $dep3);

            # Add to pool
            $this->pool[$classname] = $instance;

            return $instance;
        } else {
                return $this->pool[$classname];
        }
    }
}

Again, I would like to avoid the most costly methods to call the class. Any other suggestions?

Also, how do I access the DI class inside classes, for example, in controllers that need to access different models? Should I call it statically or pass it along each class that would require it? I don't think the last idea is feasible.

Thanks everyone.

+1  A: 

You should look into using an IOC container to manage your dependencies for you. A good IOC container should take care of passing dependencies between dependent contructors for you.

There is an existing question asking about IOC container options for PHP.

Bermo
+1  A: 

It looks like you are trying to roll your own dependency injection container. Why not use one that already exists, like Symfony, Crafty or Sphicy?

Wim Coenen
The goal is to avoid using any third party libraries unless absolutely required (mostly due to licensing issues).
andre
@andre: Symfony is licensed under the MIT license. Read it, it basically states "do what you want": http://www.opensource.org/licenses/mit-license.php
Wim Coenen
I'm creating my own license and do not want to give attribution to third-party libraries or software (part of the projects requirements), as such I need to create my own container.
andre
+7  A: 

[Before I start, let me say that I'm mostly a Java programmer - with only a little bit of PHP knowledge. But I'll simply try to get the most important concepts across without language specifics.]

Dependency Injection is based on two parts of code:

  1. Construction
  2. Execution

In its most extreme shape, there are no new operators to be found in the Execution part. All of them are moved into the Construction part. (In practice, this will be toned down.)

All of the construction happens - in the Construction part. It creates the graph of objects needed for Execution bottom up. So let's assume, it should construct A:

  • A depends on B, and
  • B depends on C.

Then

  • C is constructed first.
  • Then B is constructed with C as a parameter.
  • Then A is constructed with B as a parameter.

So C doesn't have to be passed as a constructor parameter to A. This small example doesn't illustrate strongly enough, how much this reduces the amount of objects that have to be passed around to quite a small number.

The Dependency Injector itself should not be passed into the Execution part. This is one of the basic mistakes everyone (including myself) tries to make, when they first come in contact with DI. The problem is, that this would completely blur the lines between Construction and Execution. Another way to say it is, that it would violate the Law of Demeter. Or in pattern speak: It would eventually "degrade" the Dependency Injection pattern to the Service Locator pattern. It's debatable, if this is really a degradation, but in any case it's usually not a good idea to misuse the Dependency Injector as a Service Locator.

So whenever you need to give one of your constructed objects the capability to produce other objects during execution, instead of passing the Dependency Injector, you would only pass simple Providers (a term used by the Java DI framework Guice). These are rather simple classes that can only create a certain kind of object. They have similarities with a factory.

First try to pass the required dependencies directly to the constructor.

So, to sum it up:

  • Build objects bottom-up.
  • Only pass as few dependencies as required to create an object.
  • Once your done, start executing.
  • During execution, you can still fetch newly created objects by using Providers.

But don't take it too far: Simple objects can still be created without a Provider :-)

And now, all you'll have to do is to translate this stuff into quality code. Maybe others can help you out with a few PHP examples.

Addendum: A little bit more about Providers

As noted above, the notion "Provider" (a specialized factory) is a bit specific to the Java DI framework Guice. This framework can automatically create a Provider for any type of object. However, the concept is generally useful for DI. The only difference is, that without the help of Guice or a similar framework, you'll have to write the Providers yourself - but that's quite easy:

Let's say, B depends on C.

  • If B just needs one fixed instance of C, then you don't need a Provider - you can simply construct B with the constructor argument C.
  • If B needs to create more instances of C during execution, then just write a class called CProvider with a get() method, that can create a new instance of C. Then pass an instance of CProvider into the constructor of B, and store the Provider in an instance field of B. Now B can call cProvider.get() when it needs a new instance of C.

Providers are part of the Construction code, so you're allowed to use new C(...)! On the other hand, they're not part of the Execution code, so you shouldn't have any execution logic there.

CProvider can be passed into multiple constructors of course. You can also write multiple versions CProvider1, CProvider2, ... - where each can construct different versions of C objects with different properties. Or you simple instantiate CProvider multiple times with different arguments.

Chris Lercher
Insightful, thanks for the explanation. I know about the Service Locator pattern and that's not what I'm trying to accomplish. Could you please elaborate a bit on the usage of providers?
andre
Added an addendum. Let me know, if you need more info.
Chris Lercher
Thanks a lot for the help. Basically I need to pass along the required providers for each multiple-instanced dependency. These are basically Factory patterns but specific to create objects for a purpose (could be a variety of classes, an example would be output drivers) and return them to the dependent class. Sounds like more complexity incoming :D
andre
I think, the initial effort is well invested - because when using DI (with or without a framework), you'll end up with components that you can even reuse in your next application! That's IMO the greatest benefit of DI in the long run, even more important than the increased testability of your code :-)
Chris Lercher
Fantastic explanation. In C# I like to just use Func<T> as my providers. It's very simple.
Mark Lindell