bonzer/ioc-container

Inversion of Control Container - facilitating Interface Oriented Design

v0.0.2 2023-09-10 16:09 UTC

This package is not auto-updated.

Last update: 2024-04-22 10:21:10 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 or get_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.