DEV Community

Cover image for automatic dependency injection in 4/5 lines of code
Simone Gentili
Simone Gentili

Posted on

automatic dependency injection in 4/5 lines of code

The project

You can find the code here.

Prerequisite

You just have installed composer due to this script uses psr-4 autoloading.

Usage

If you have a more complex situation like this

class Foo {}
class Bar { public function __construct(Foo $foo) {} }
class Pub {
  public function __construct(Bar $bar) {}
  public function method() {
    return 42;
  }
}
class Animal { }
class Cat { public function __construct(Animal $animal) {} }
class Fizz {
  public function __construct(private Pub $pub) {}
  public function doSomething(Cat $cat) {
    return $this->pub->method();
  }
}
Enter fullscreen mode Exit fullscreen mode

You can instance Fizz object and the run doSomething method just doing

$result = injector(Fizz::class, 'doSomething');
Enter fullscreen mode Exit fullscreen mode

5 line solution

This solution is not readable. Is just to create a clickbait.

function injector($instance, $method = '__construct', $deps = []) {
    foreach ((new \ReflectionMethod($instance, $method))->getParameters() as $dep) {
        $className = (string) $dep->getType();
        $deps[] = new $className;
    }
    return $method == '__construct' ? new $instance(...$deps) : injector($instance)->$method(...$deps);
}
Enter fullscreen mode Exit fullscreen mode

Readable solution

This is a readable solution.

function buildDeps($instance, $method, $deps = []) {
    $params = (new \ReflectionMethod($instance, $method))
        ->getParameters();
    foreach ($params as $dep) {
        $className = (string) $dep->getType();
        $deps[] = new $className;
    }
    return $deps;
}

function injector($instance, $method = '__construct') {
    $deps = buildDeps($instance, $method);
    return $method == '__construct'
        ? new $instance(...$deps)
        : injector($instance)->$method(...$deps);
}
Enter fullscreen mode Exit fullscreen mode

And if you want to see the entire process of its creation (but, sorry, only in italian) click here.

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Tiugo image

Fast, Lean, and Fully Extensible

CKEditor 5 is built for developers who value flexibility and speed. Pick the features that matter, drop the ones that don’t and enjoy a high-performance WYSIWYG that fits into your workflow

Start now

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay