Download the PHP package corviz/di-container without Composer
On this page you can find all versions of the php package corviz/di-container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download corviz/di-container
More information about corviz/di-container
Files in corviz/di-container
Package di-container
Short Description A simple yet powerful container with auto wiring
License MIT
Informations about the package di-container
di-container
A simple yet powerful container with auto wiring
Installation
Usage
Here are some common scenarios you will meet when using container + dependency injection
For these examples, we will assume the following classes:
Retrieve a new instance from container
Note that no previous declaration was required!
Auto-wiring and parameter types
What if we need a new instance of B?
Because we declared a parameter with type "A" in the constructor, and it is a class, our container will try to create a new instance automatically. This is done though Reflection.
If we needed to receive a primitive type as parameter, we have to declare a default value otherwise the container will throw a ContainerException, since it can't guess how to fill it.
Manual setup
You may want to set some objects manually in some particular cases. To do so, simply use 'set' method.
Aliases
If we want to alias something, all we have to do is set it in our container as usual
Singletons
When we want to access the same object through the entire execution, all we have to do is use 'setSingleton'. It accepts the same definition variants as 'set'. In other words, you may use the class name, a constructor closure or an object instance.
Invoking methods
Let's assume the following class: