Frage

I need to make a DI container which will automatically inject the needed dependencies.

I only have one idea to do this, Lets say i have initialized a controller using the container, the container will get the namespace of that object and then it will detect what dependencies it requests. The container should get the namespaces of the dependencies that needs to be injected, and then it will create them, then create a new object witg these depndencies and return it.

But the question is, how can you check what dependencies the constructor requests in php?

War es hilfreich?

Lösung

ReflectionFunction::getParameters() or ReflectionMethod::getParameters() return an array of ReflectionParameter instances.

Now on the ReflectionParameter instances you can use ReflectionParameter::getClass() to get the classes you need.

To get the class' name, you need to access the name property of that ReflectionClass which was returned by ReflectionParameter::getClass().

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top