Dependency Injection: a discussion of the pros and cons

how exactly is:

entrypoint.php

new Controller(new Model);

Less flexible than

entrypoint.php

$model_name = 'model';
requrire_once 'controller.php';

controller.php

$model = new $model_name;

//...

I can choose any controller and any model in the entry point in both examples.

The difference is, when using the first example I can also do this:

new Controller(new Model(new Address));

using the two-file method, I cannot choose the dependency in the “transaction script”

2 Likes