📜 ⬆️ ⬇️

PHP IoC

Inversion of Control (IoC) containers are a convenient way to organize dependency injection and is widely used in the Java world.

This library allows you to use IoC containers in PHP.


')
For example, we describe several interfaces:

interface First { public function first(); } interface Second { public function second(); } interface Third { public function third(); } 


And their implementations:

 class Foo implements First { public function first() { /* ... */ } } class Boo implements Second { public function __construct(First $first) { /* ... */ } public function second() { /* ... */ } } class Woo implements Third { public function __construct(First $first, Second $first) { /* ... */ } public function third() { /* ... */ } } 


Now we will use the IoC container to describe dependencies:

 $ioc = IoC\Container::getInstance(); $ioc->register('Foo'); $ioc->register('Boo'); $ioc->register('Woo'); 


Dependencies are assigned automatically. The Foo class implements the First interface: in the IoC container, First refers to Foo.

We describe a class manager depending on these interfaces:

 class Manager { use IoC\Creatable; public function __construct(First $first, Second $second, Third $third) { /* ... */ } } 


Create a manager instance:

 $manager = Manager::create(); 


IoC itself will create copies of the necessary classes for the designer of the manager.

If there is no PHP 5.4, then you can not use impurities:

 class Manager { public static function create() { return \IoC\Factory::create(get_called_class(), func_get_args()); } /* ... */ } 


Also in the designer of the manager it is possible to describe additional parameters:

 class Manager { use IoC\Creatable; public function __construct(First $first, Second $second, Third $third, $value, $anotherValue = 'default') { /* ... */ } } 


And still use IoC:

 $manager = Manager::create('value', 'another value'); $managerWithDefault = Manager::create('value'); 


Three types of dependencies are implemented in the library: Reference, Lazy and Prototype.

Lazy loading:
 $ioc->register('Foo'); 

An instance of the Foo class will be created only when the create () function is called.

In this case, one instance of the class Foo will be used everywhere:
 $ioc->register(new Foo()); 


If the class is built on a prototype template (new objects are created using clone), then the following function will be useful:
 $ioc->prototype(new Foo()); 

Every time create () is called, a new instance of Foo will be created with clone.

You can also add your own associations:
 $ioc->assoc(new MyAssoc('Foo')); 


IoC allows you to manually configure compliance with the implementation of the interface:

 $ioc->register('Foo', 'First'); $ioc->register(new Foo(), 'Second'); $ioc->prototype('Foo', 'First'); 


Multiple association. If the FooBoo class implements two First, Second interfaces at once:

 $ioc->register('FooBoo', array('First', 'Second')); 


You can also add associations for classes:

 //  Boo extends Foo $ioc->register('Boo', 'Foo'); 


GitHub project

Other implementations of IoC for PHP

PHP port Pico Container
Phemto
Sharbat

Source: https://habr.com/ru/post/132084/


All Articles