PHPackages                             webx/ioc - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. webx/ioc

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

webx/ioc
========

2.0.0(9y ago)01.3k↓25%11MITPHP

Since Nov 1Pushed 8y ago1 watchersCompare

[ Source](https://github.com/niclaslindberg/webx-ioc)[ Packagist](https://packagist.org/packages/webx/ioc)[ RSS](/packages/webx-ioc/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (28)Used By (1)

WebX-Ioc - PHP IOC Container
============================

[](#webx-ioc---php-ioc-container)

Why choose WebX-Ioc

- Extremely flexible.
- Very fast &amp; light weight (&lt; 170 lines, lazy initialization, resolution cache).
- No external dependencies.
- Easy to integrate - Built in closure invokation and instantiation by class name.

Installing
----------

[](#installing)

Packagist: `webx/ioc`

Getting started
---------------

[](#getting-started)

To get started the IOC container must be initialized and implementations must be registered.

```
    use WebX\Ioc\Ioc;
    use WebX\Ioc\Util\Bootstrap;   //Ready to use bootstrapper.
```

#### Resolving an instance by interface

[](#resolving-an-instance-by-interface)

```
    class ClassA implements InterfaceA {}

    $ioc = Bootstrap::ioc();
    $ioc->register(ClassA::class);

    $a = $ioc->get(InterfaceA::class);
    // objectA - instance of classA (implements InterfaceA).
    $a2 = $ioc->get(InterfaceA::class);
    echo($a===$a2); // true
```

#### Resolving multiple instances of the same interface

[](#resolving-multiple-instances-of-the-same-interface)

```
    class ClassA implements InterfaceA {}
    class ClassAB implements InterfaceA, InterfaceB {}

    $ioc = Bootstrap::ioc();
    $ioc->register(ClassA::class);
    $ioc->register(ClassAB::class);

    $allA = $ioc->getAll(InterfaceA::class);
    // [objectA,objectAB] - array of all instances of InterfaceA.

    $allB = $ioc->getAll(InterfaceB::class);
    // [objectAB] - array of all instances of InterfaceB.
```

#### Resolving same instance from multiple interfaces

[](#resolving-same-instance-from-multiple-interfaces)

```
    class ClassAB implements InterfaceA,InterfaceB {}

    $ioc = Bootstrap::ioc();
    $ioc->register(ClassAB::class);

    $a = $ioc->get(InterfaceA::class);
    $b = $ioc->get(InterfaceB::class);
    echo($a===$b); // true
```

#### Registering an already existing instance

[](#registering-an-already-existing-instance)

The container supports registration of already existing instances to be resolved by their interfaces.

```
    class ClassA implements InterfaceA {}

    $a = new ClassA(); // Instantiated outside the container.
    $ioc = Bootstrap::ioc();
    $ioc->register($a);

    $a2 = $ioc->get(InterfaceA::class);
    echo($a===$a2); // true
```

### Configuring instances

[](#configuring-instances)

Web/Ioc allows instances to be configured. Configuration is done with the optional configuration array on the 'register()' function. All values are optional.

```
    $config = [
        "id" => (string)"someId",
        // Unique id (per interface type) for the registered instance.
        "parameters" => (array) [
            "constructorParam1" => (string)
            "constructorParamN" => (string)
            //If constructor parameter is
            array and the value is string it will be used as interface-type-hint for resolving instances of type.
            class and the value is string it will be used as id to find class instance
            else the value will be set for the contructor param
        ],
        "factory" => (Closure) control instantiation by a dependency injection supported Closure.
            //Ex: function(IA $resolvedInstance) {
            //  return new ClassA($resolvedInstance)
            //}
            //Note: "parameters" are supported for factory closure arguments.
        "class" => bool //If the container should also publish the instance by it's class name
    ];

    $ioc->register(SomeClass::class,$config);
```

#### Registering a named instance or class

[](#registering-a-named-instance-or-class)

```
    class ClassA implements InterfaceA {}

    $ioc = Bootstrap::ioc();
    $ioc->register(ClassA::class,["id"=>"id1"]);
    $ioc->register(ClassA::class,["id"=>"id2"]);

    $a1 = $ioc->get(InterfaceA::class,"id1");
    $a2 = $ioc->get(InterfaceA::class,"id2");
    echo($a1 !== $a2); // true
```

#### Resolving an instance by its class name

[](#resolving-an-instance-by-its-class-name)

```
    class ClassA implements InterfaceA {}

    $ioc = Bootstrap::ioc();
    $ioc->register(ClassA::class,["class"=>true]);

    $a1 = $ioc->get(ClassA::class);
    echo($a1 instanceof ClassA); // true
```

#### Registering a named instance and configuring a mapping to it

[](#registering-a-named-instance-and-configuring-a-mapping-to-it)

```
    class ClassA implements InterfaceA {}

    class ClassB implements InterfaceB {

        public $a;

        public function __construct(InterfaceA $paramA) {
            $this->a = $a;
        }
    }

    $ioc = Bootstrap::ioc();
    $a1 = new ClassA();
    $a2 = new ClassA();
    $ioc->register($a1,["id"=>"id1"]);
    $ioc->register($a2,["id"=>"id2"]);

    $ioc->register(ClassB::class,["parameters"=>["paramA"=>"id2"]]);
    //Causes the constructor param 'paramA' of class 'ClassB' to use instance 'id2'

    $b = $ioc->get(InterfaceB::class);
    echo($a2 === $b->a); // true
```

#### Registering an instance with a predefined constructor parameter

[](#registering-an-instance-with-a-predefined-constructor-parameter)

```
    class ClassA implements InterfaceA {

        public $someVar;
        public $b;

        public function __construct($someVar,InterfaceB $b) {
            $this->someVar = $someVar;
            $this->b = $b;
        }

    }

    $ioc = Bootstrap::ioc();
    $ioc->register(ClassA::class,["parameters"=>["someVar"=>"someValue"]]);
    $ioc->register(ClassB::class);

    $a = $ioc->get(InterfaceA::class);
    echo($a->someVar); // "someValue"
```

#### Controlling instantiation with a factory closure

[](#controlling-instantiation-with-a-factory-closure)

```
    class ClassA implements InterfaceA {}

    class ClassB implements InterfaceB {}

    $ioc = Bootstrap::ioc();
    $ioc->register(ClassB::class);
    $ioc->register(InterfaceA::class, ["factory" => function(InterfaceB $b){ //Scans the concrete class
                                                                         //ClassA for interfaces
        return new ClassA($b);
    }]);
    $a = $ioc->get(InterfaceA::class);
```

#### Instantiate a non-registered class

[](#instantiate-a-non-registered-class)

If you want to instantiate a class with dependency injection

```
    class ClassA implements InterfaceA {

        public function __construct() {}

        public function sayWhat() {
            return "Here I am!";
        }
    }

    class ClassB  {

        private $a;

        public function __constructor(InterfaceA $a) {
            $this->a = $a;
        }
        public function saySomething() {
            return $this->a->sayWhat;
        }
    }

    $ioc = Bootstrap::ioc();
    $ioc->register(ClassA::class);

    $b = $ioc->instantiate(ClassB::class);
    echo($a->saySomething()); // "Here I am!"
```

#### Invoke a a `Closure`

[](#invoke-a-a-closure)

Closures may be invoked with its dependencies automatically resolved

```
    class ClassA implements InterfaceA {

        public function __construct() {}

        public function sayWhat() {
            return "Here I am!";
        }
    }

    $ioc = Bootstrap::ioc();
    $ioc->register(ClassA::class);

    $result = $ioc->invoke(function(InterfaceA $a) {
        return $a->sayWhat();
    });
    echo($result); // "Here I am!"
```

### Resolving non-resolvable parameters

[](#resolving-non-resolvable-parameters)

WebX/Ioc recursively tries to resolve all dependent interfaces upon object creation. Other dependencies must be resolved externally.

#### Example 1

[](#example-1)

```
    class ClassA implements InterfaceA {
        private $b;
        private $currency;

        public function __construct(InterfaceB $b, $currency="EUR") {
        //$b is automatically resolved by the container (ClassB is registered).
        //$currency is not an interface and will be resolved by the resolver function
            $this->b = $b;
            $this->currency = $currency;
        }
        public function currency() {
            return $currency;
        }
    }

    // Will be invoked whenever the container needs
    // to resolve an non-resolvable parameter.
    $resolver = function(IocNonResolvable $nonResolvable, Ioc $ioc) {
        if($param->name()==='currency') {
            return "USD";
        }
    };

    Bootstrap::init($resolver);
    $iocWithResolver = Bootstrap::ioc();
    $iocWithResolver->register(ClassA::class);
    $iocWithResolver->register(ClassB::class);
    $a = $iocWithResolver->get(InterfaceA::class);
    echo($a->currency());
    //Returns ClassA's resolved value for $currency "USD"

    $ioc = Bootstrap::ioc();
    $ioc->register(ClassA::class);
    $ioc->register(ClassB::class);
    $a = $ioc->get(InterfaceA::class);
    echo($a->currency());
    //Returns ClassA's default value for $currency "EUR"
```

### Utilities

[](#utilities)

- `WebX\Ioc\Util\Bootstrap` Simple, easy to use bootstrapper for a single shared instance of Ioc. Statically accessible.

### How to run tests

[](#how-to-run-tests)

In the root of the project:

```
    composer install
    phpunit -c tests
```

### Related projects

[](#related-projects)

- `webx/db`
- `webx/routes`

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~22 days

Recently: every ~81 days

Total

26

Last Release

3298d ago

Major Versions

1.4.2 → 2.0.02017-05-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/bba7dfe2d222db40ed75a5eb27347e437d3d1129d15c6822d0e46be6ee736df0?d=identicon)[niclaslindberg](/maintainers/niclaslindberg)

---

Top Contributors

[![niclaslindberg](https://avatars.githubusercontent.com/u/4067396?v=4)](https://github.com/niclaslindberg "niclaslindberg (1 commits)")

---

Tags

diioc

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/webx-ioc/health.svg)

```
[![Health](https://phpackages.com/badges/webx-ioc/health.svg)](https://phpackages.com/packages/webx-ioc)
```

###  Alternatives

[level-2/dice

A minimalist Dependency injection container (DIC) for PHP. Please note: 3.0+ is only compatible with PHP 7.0. The 2.0 branch is compatbile with PHP 5.6.

437730.3k17](/packages/level-2-dice)[hyperf/di

A DI for Hyperf.

182.8M594](/packages/hyperf-di)[x-wp/di

The dependency injection container for WordPress

301.1k10](/packages/x-wp-di)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
