bonzer / ioc-container
Inversion of Control Container - facilitating Interface Oriented Design
Requires
- php: >=5.4.0
- bonzer/exceptions: v0.0.1
This package is not auto-updated.
Last update: 2025-03-10 14:16:28 UTC
README
Facilitating Interface Oriented Design
Getting Started
Prerequisites
- PHP >= 5.4
- "bonzer/exceptions" : "dev-master"
Installing
It can be installed via composer. Run
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:
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:
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.
Instantiating
For instantiating, Just call App_Container::make
with the bound key:
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.
App_Container::make('My\Namespace\My_Class');
for singleton instantiation without binding, use:
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
orget_instance
Support
If you are having issues, please let me know.
You can contact me at ralhan.paras@gmail.com
License
The project is licensed under the MIT license.