Download the PHP package mohamed-amine/ioc-container without Composer
On this page you can find all versions of the php package mohamed-amine/ioc-container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mohamed-amine/ioc-container
More information about mohamed-amine/ioc-container
Files in mohamed-amine/ioc-container
Package ioc-container
Short Description Inversion of control container
License MIT
Informations about the package ioc-container
How to install:
Via composer
What can it do:
- create instances and resolve their dependencies automatically.
- resolve dependencies from types( like Interfaces) if they are registered.
- create instances from registred services locators.
- bind existing instances into the container (can be used for service providers).
- call instances from the container anywhere in your application.
How to use it:
Create the contianer:
in order to use all the features we need to create a container first
Create an instance in the container:
-
to create an instance and resolve it's dependencies automatically:
This will create the MyClass object and create all of it's dependencies recursively for example:
In order for this to work you need to use type hinting in MyClass constructor :
- if the instance require arguments we should pass them in an array as a second argument of 'build()' (note that automatic dependencies resolving can't be used in this case):
Bind instances into the container:
To bind an existing instance into the container
Now we will be able to access our instance anywhere in the application like this:
Build instances from services locators (short names):
We can define service locators using the regiter method
Then we can build an instance using this service locator
Registering Types:
Types are registered to help the container to resolve classes dependencies, for example if a class A have a dependency of Type SomeInterface which is an interface, we will have to register this type first in order to be able to resolve it:
Calling instances from the container:
we can call instances from the container using 2 methods