Ben Beri
Ben Beri

Reputation: 1201

PHP DI container - reading what objects the constructor requests

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?

Upvotes: 0

Views: 55

Answers (1)

bwoebi
bwoebi

Reputation: 23777

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().

Upvotes: 1

Related Questions