mohamed-amine / ioc-container
Inversion of control container
Requires
- php: ^8.3
- phpunit/phpunit: ^11.1
This package is auto-updated.
Last update: 2025-06-07 01:36:28 UTC
README
How to install:
Via composer
composer require mohamed-amine/ioc-container
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
IOC\IOC::createContainer();
Create an instance in the container:
- 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);
Bind instances into the container:
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');
Build instances from services locators (short names):
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');
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:
IOC\IOC::container()->registerType('SomeInterface', Namespace\SomeClass::class);
Calling instances from the container:
we can call instances from the container using 2 methods
IOC\IOC::container()->MyInstance; // Or IOC\IOC::container()->get('MyInstance');