2017 © Pedro Peláez
 

library ioc-container

Inversion of control container

image

mohamed-amine/ioc-container

Inversion of control container

  • Friday, March 23, 2018
  • by MohamedAmine-C
  • Repository
  • 2 Watchers
  • 1 Stars
  • 27 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 4 % Grown

The README.md

Build Status, (*1)

How to install:

Via composer

composer require mohamed-amine/ioc-container, (*2)

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, (*3)

  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: php // 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 : php ... 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):, (*4)

  IOC\IOC::container()->build('namespace\MyClass', $argument1, $argument2);

Bind instances into the container:

To bind an existing instance into the container, (*5)

  IOC\IOC::bind('MyClass',function() {
    return new namespace\MyClass();
  });

Now we will be able to access our instance anywhere in the application like this:, (*6)

  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, (*7)

  IOC\IOC::container()->register('MyClass', namespace\MyClass::class);

Then we can build an instance using this service locator, (*8)

  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:, (*9)

  IOC\IOC::container()->registerType('SomeInterface', Namespace\SomeClass::class);

Calling instances from the container:

we can call instances from the container using 2 methods, (*10)

  IOC\IOC::container()->MyInstance;
  // Or
  IOC\IOC::container()->get('MyInstance');

The Versions

23/03 2018

dev-develop

dev-develop

Inversion of control container

  Sources   Download

MIT

The Requires

 

23/03 2018

dev-master

9999999-dev

Inversion of control container

  Sources   Download

MIT

The Requires

 

22/03 2018

v0.5.0

0.5.0.0

Inversion of control container

  Sources   Download

MIT

The Requires

 

22/03 2018

v0.5.0.x-dev

0.5.0.9999999-dev

Inversion of control container

  Sources   Download

MIT

The Requires

 

22/03 2018

dev-phpunit&travis-tests

dev-phpunit&travis-tests

Inversion of control container

  Sources   Download

MIT

The Requires

 

16/03 2018

dev-add-comments

dev-add-comments

Inversion of control container

  Sources   Download

MIT

The Requires

  • php >=7.0

 

28/02 2018

v0.4

0.4.0.0

Inversion of control container

  Sources   Download

MIT

The Requires

  • php >=7.0

 

28/02 2018

v0.4.x-dev

0.4.9999999.9999999-dev

Inversion of control container

  Sources   Download

MIT

The Requires

  • php >=7.0

 

04/02 2018

v0.3.3

0.3.3.0

Inversion of control container

  Sources   Download

MIT

The Requires

  • php >=7.0

 

02/02 2018

v0.3.2

0.3.2.0

Inversion of control container

  Sources   Download

MIT

The Requires

  • php >=7.0

 

30/01 2018

v0.3.1

0.3.1.0

Inversion of control container

  Sources   Download

MIT

The Requires

  • php >=7.0

 

30/01 2018

v0.3

0.3.0.0

Inversion of control container

  Sources   Download

MIT

The Requires

  • php >=7.0

 

29/01 2018

v0.2

0.2.0.0

PHP dependency injection container

  Sources   Download

MIT

The Requires

  • php >=7.0

 

22/01 2018

v0.1

0.1.0.0

PHP dependency injection container

  Sources   Download

MIT

The Requires

  • php >=7.0

 

20/01 2018

0.0.1

0.0.1.0

PHP dependency injection container

  Sources   Download

MIT

The Requires

  • php >=7.0