Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Inversion of Control Container for PHP
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src/IoC
test
.gitignore
README.md
composer.json
composer.lock

README.md

IoC for PHP

Wiki: Inversion of Control (IoC) is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming.

This is implementation of IoC for PHP.

Usage

Describe interfaces:

<?php
interface First {
    public function first();
}

interface Second {
    public function second();
}

interface Third {
    public function third();
}

Describe classes:

<?php
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() { /* ... */ }
}

Describe associations with IoC:

<?php
$ioc = IoC\Container::getInstance();

$ioc->register('Foo');
$ioc->register('Boo');
$ioc->register('Woo');

And trait IoC\Creatable to any classes:

<?php
class Manager
{
    use IoC\Creatable;

    public function __construct(First $first,
                                Second $second,
                                Third $third)
    {
        /* ... */
    }
}

and create objects with easy:

<?php

$manager = Manager::create();

Is it possible to use without trait? YES!

Add static create method manualy:

<?php
class Manager
{
    public static function create()
    {
        return \IoC\Factory::create(get_called_class(), func_get_args());
    }

    /* ... */
}

You can have another parameters in constructor:

<?php
class Manager
{
    use IoC\Creatable;

    public function __construct(First $first,
                                Second $second,
                                Third $third,
                                $value,
                                $anotherValue = 'default')
    {
        /* ... */
    }
}

and still have simple create function:

<?php

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

/* ... */

$managerWithDefault = Manager::create('value');

You can create this associations: Reference, Lazy and Prototype.

<?php
$ioc = IoC\Container::getInstance();

// This is Lazy assoc.
// Every time then you call create() it will call Foo::create().
$ioc->register('Foo');

/* ... */

// This is Reference assoc.
// This object will be posted everywhere.
$ioc->register(new Foo());

/* ... */

// This is Prototype assoc.
// It usefull for classes what implementation Prototype Pattern.
// Every time then you call create() it will clone this object,
// so Foo class must implement __clone() function.
$ioc->prototype(new Foo());

/* ... */

// Custom asscc.
// You can create your own asscc (must extend IoC\Assoc\Assoc)
$ioc->assoc(new MyAssoc('Foo'));

Also you can manually specify assoc interfaces and classes:

<?php
$ioc = IoC\Container::getInstance();

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

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

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

// If Foo implements First, Second
$ioc->register('Foo', array('First', 'Second'));

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

License

IoC for PHP is licensed under the MIT license.

Something went wrong with that request. Please try again.