composer require mohamed-amine/ioc-container
- 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.
in order to use all the features we need to create a container first
IOC\IOC::createContainer();
- to create an instance and resolve it's dependencies automatically:
IOC\IOC::container()->build('namespace\MyClass');
This will create the MyClass object and create all of it's dependencies recursively for example:
// instead of doing this
new MyClass(new ClassA(), new ClassB());
// you can just do this and it will create ClassA & ClassB and pass them automatically
IOC\IOC::container()->build('namespace\MyClass');
In order for this to work you need to use type hinting in MyClass constructor :
...
public function __constructor(ClassA $classa, ClassB $classb) {...}
...
- 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):
IOC\IOC::container()->build('namespace\MyClass', $argument1, $argument2);
To bind an existing instance into the container
IOC\IOC::bind('MyClass',function() {
return new namespace\MyClass();
});
Now we will be able to access our instance anywhere in the application like this:
IOC\IOC::container()->MyClass;
// or
IOC\IOC::container()->get('MyClass');
We can define service locators using the regiter method
IOC\IOC::container()->register('MyClass', namespace\MyClass::class);
Then we can build an instance using this service locator
IOC\IOC::container()->build('MyClass');
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:
IOC\IOC::container()->registerType('SomeInterface', Namespace\SomeClass::class);
we can call instances from the container using 2 methods
IOC\IOC::container()->MyInstance;
// Or
IOC\IOC::container()->get('MyInstance');