Jethro Hazelhurst
Jethro Hazelhurst

Reputation: 3285

Accessing Dependencies with a Foreach Loop

I am trying to make a simple dependency 'mapper', I don't think I can even call it a dependency injector... so I have tried to make a minimal proof of concept below.

on my index.php page I have the following...

// >>> psr-4 autoloader here

// instantiating the container
$container = new Container;

// getting an instance of 'A' with an object tree of A->B->C
$a = $container->get('A');

In my simple container I have this...

class Container
{
    public $dependencies = []; // an array of dependencies from the dependency file

    public function __construct()
    {
        // include list of dependencies
        include 'Dependencies.php';

        foreach ($dependency as $key => $value) {
            $this->dependencies[$key] = $value; // e.g. dependency['A'] = ['B'];
        }
    }

    /**
     * gets the dependency to instantiate
     */
    public function get($string)
    {

        if (isset($this->dependencies[$string])) {

            $a = $string;

            foreach ($this->dependencies[$string] as $dependency) {

                $b = $dependency;

                if (isset($this->dependencies[$dependency])) {

                    foreach ($this->dependencies[$dependency] as $dependency);

                    $c = $dependency;

                }

            }

        }

        $instance = new $a(new $b(new $c));
        return $instance;

    }
}

I have my dependencies mapped out in a separate file that looks like this...

/**
 * to add a dependency, write the full namespace
 */

$dependency['A'] = [
    'B' // A depends on B
];

$dependency['B'] = [
    'C' // which depends on C
];

And a set of classes, A, B and C which depend on eachother...

class A
{
    public function __construct(B $b)
    {
        echo 'Hello, I am A! <br>';
    }
}



class B
{
    public function __construct(C $c)
    {
        echo 'Hello, I am B!<br>';
    }
}



class C
{
    public function __construct()
    {
        echo 'Hello, I am C! <br>';
    }
}

I have tried all afternoon to get something to work, but I am afraid either I am not thinking clearly enough

Question

So in my get() function, how can I make the loading of these dependencies automatic, so that my get function is not just a never ending nest of foreach and ifelse statements... do I need to add some sort of callback? It really is not clear to me, and I must stress that I have tried quite a few different approaches which did not work, too many to include.

Upvotes: 0

Views: 423

Answers (1)

Constantin Galbenu
Constantin Galbenu

Reputation: 17683

You are doing it wrong. Your container is designed to work only with that dependency structure. For example, $container->get('B') will not work, neither $container->get('C') because $c will be null and third nested new will fail.

I suggest that you make your container as a recursive function.

BTW, what's with the third foreach? Are you trying to get the last dependency?

You can read my article.

Upvotes: 1

Related Questions