IOC Container -- Inversion of Control Container
Facilitating Interface Oriented Design, (*1)
Getting Started
Prerequisites
- PHP >= 5.4
- "bonzer/exceptions" : "dev-master"
Installing
It can be installed via composer. Run, (*2)
composer require bonzer/ioc-container
Usage
use Bonzer\IOC_Container\facades\Container as App_Container;
Binding to Container
There are two methods available:
bind
or singleton
for binding your class with container.
used as:, (*3)
App_Container::bind('My_Class', 'My\Namespace\My_Class');
App_Container::singleton('My_Class', 'My\Namespace\My_Class');
Binding Interfaces:
You can also bind your interfaces with container as:, (*4)
App_Container::bind('My\Namespace\My_Interface', 'My\Namespace\My_Class');
and whenever you use this interfaces in any other class and if that class is bound and/or instantiated via IOC_Container
the interface will automatically gets resolved., (*5)
Instantiating
For instantiating, Just call App_Container::make
with the bound key:, (*6)
App_Container::make('My_Class');
-
Note: If you have bound with
singleton
, App_Container::make
will return the same instance everytime.
Direct Instantiation:
Direct Instantiation is also possible, without binding. For eg., (*7)
App_Container::make('My\Namespace\My_Class');
for singleton instantiation without binding, use:, (*8)
App_Container::make_singleton('My\Namespace\My_Class');
-
Note: If your class has private constructor, then
IOC_Container
checks for the availability of any of the two static methods: init
or get_instance
Support
If you are having issues, please let me know.
You can contact me at ralhan.paras@gmail.com, (*9)
License
The project is licensed under the MIT license., (*10)