PHPackages                             laborra/php-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. [Framework](/categories/framework)
4. /
5. laborra/php-ioc

ActiveLibrary[Framework](/categories/framework)

laborra/php-ioc
===============

PHP framework for managing dependency injection

0.2.1(12y ago)320BSD-3-ClausePHP

Since Nov 4Pushed 12y ago2 watchersCompare

[ Source](https://github.com/zeeke/php-ioc)[ Packagist](https://packagist.org/packages/laborra/php-ioc)[ RSS](/packages/laborra-php-ioc/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)Dependencies (2)Versions (6)Used By (0)

PHP Inversion of Control
========================

[](#php-inversion-of-control)

IoC framework for PHP

[![Build Status](https://camo.githubusercontent.com/a9681211f0fded89f076a19650c452480926785d40d9cd739aaa753bcbb88d45/68747470733a2f2f7472617669732d63692e6f72672f7a65656b652f7068702d696f632e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/zeeke/php-ioc)[![Coverage Status](https://camo.githubusercontent.com/6f90584440c2182d5a69605f69f8e622399ebe568d8617bc95906231a9082249/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f7a65656b652f7068702d696f632f62616467652e706e67)](https://coveralls.io/r/zeeke/php-ioc)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/ddd25f1dd6d03e23861ff827da2682c3b9c54b0b276eab1675fdb3907dd65475/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7a65656b652f7068702d696f632f6261646765732f7175616c6974792d73636f72652e706e673f733d31306532376234386162346634343066343561616162376133333766626462363730373066356631)](https://scrutinizer-ci.com/g/zeeke/php-ioc/)

Using an Inversion Of Control container you can define the business logic of an application without depending on the specific framework. All the core logic can be placed in simple PHP classes, where dependency are injected by the container.

Installation
------------

[](#installation)

The library can be installed using Composer:

```
# Install Composer
curl -sS https://getcomposer.org/installer | php

# Add php-ioc as a dependency (use dev-master until a stable release is out)
php composer.phar require laborra/php-ioc:dev-master

```

Basic Usage
-----------

[](#basic-usage)

The main library class is AppContext, that can be built using the ContextFactory helper,

```
$config = [
    // Bean configuration as PHP Array
    //...
];

$context = ContextFactory::buildFromPHPArray($config);

// Or

$context = ContextFactory::buildFromFile('path/to/configurationfile.php');
```

Below is an example of context configuration with a bas

```
// config.php
return [
    'beans' => [
        'beanOne' => [
            'ClassOne',
            'properties' => [
                'valueProp' => 42,
            ],
        ],
        'beanTwo' => [
            'ClassTwo',
            'properties' => [
                'refOne' => '@beanOne',
            ],
        ],

        'beanThree' => [
            'ClassThree',
            'constructorArgs' => [
                @beanTwo,
                'Answer to life the universe and everything'
            ],
        ],
    ],

]

class ClassOne
{
    public $valueProp;
}

class ClassTwo
{
    /** @var ClassOne */
    public $refOne;
}

class ClassThree
{
    /** @var ClassTwo */
    private $refTwo;

    /** @var string */
    private $strProp

    public function __constructor (ClassTwo $refTwo, $strAtg)
    {
        $this->strProp = $strArg;
    }

    public function getIt ()
    {
        return $this->strProp.' = '.$this->refTwo->refOne->valueProp;
    }
}

// In application logic

$beanObj = $context->getBean('beanThree');
echo $beanObj->getIt(); // Answer to life the universe and everything = 42
```

Features
--------

[](#features)

The current supported features are:

- Bean definition as request singleton scope
- Definition of relationships between beans
- Constructor arguments
- Class calls after bean creation
- Definition of context parameters
- Configuration based on PHP array, ConfigurationBuilder helper and YAML files

Features available in future releases:

- Prototype scope for beans
- Support for Aspect Oriented Programming
- Cache for configuration and application context
- Support for multiple configuration files

Defining Application Modules
----------------------------

[](#defining-application-modules)

Using an IoC container it is possible to build modularized applications. Each module consists of a set of library classes, a set of dependency and list of bean that must be implemented by the module client. In this way, you can define an entire unit of business logic without referencing any framework or specific environment.

### Example of user management module

[](#example-of-user-management-module)

```
class UserService
{
    /** @var IUserDAO
    public $userDAO;

    public function authenticate ($username, $password)
    {
        // Business logic to authenticate the user
        $user = $this->userDAO->getByUserName($username);

        if ($user == null) {
            throw new Exception("User $username not found");
        }

        if ($user->password != $password) {
            throw new Exception("Invalid credentials");
        }

        return $user;
    }

    public function register ($username, $password)
    {
        if ($username == "") {
            throw new UserException("Username field cannot be blank");
        }

        if ($password == "") {
            throw new UserException("Password field cannot be blank");
        }

        $user = $this->userDAO->create($username, $password);
        return $user;
    }
}

interface IUserDAO
{
    function getByUsername ($username);

    function create ($username, $password);
}

class User
{
    public $username;

    public $password;
}
```

beans: userService: class: UserService properties: userDAO: @userDAO

When using the module in a complete application, we have to implement the IUserDAO interface and declare it as a context bean.

class SpecificUserDAO implements IUserDAO { function getByUsername ($username) { // Query the database, possible using framework specific helpers // ... }

```
function create ()
{
    // ...
}

```

}

beans: userDAO: class: SpecificUserDAO

ConfigurationBuilder utility
----------------------------

[](#configurationbuilder-utility)

In order to produce readable and maintenable configuration files, you can use the ConfigurationBuilder class. Below there is an example of a configuration built using this class.

```
$c = new ConfigurationBuilder();

$c->bean('beanOne', '\vendorName\library\ClassName')
    ->property('valueProp', 12)
    ->property('refProp', '@beanTwo')
    ->constructorArg('String value')
    ->constructorArg(42);

return $c->build();
```

You can extend the configuration builder class to add your own custom methods. Doing so, you can create shortcut to some very often used configuration parts.

```
class SpecificConfigurationBuilder extends ConfigurationBuilder
{
    public function beanOneRef()
    {
        return $this->prop('refProp', '@beanOneId');
    }
}
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.6% 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 ~25 days

Total

4

Last Release

4496d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1f4cc3fb52edeadd9d5c57f04ac87423306e478318cb95e917c3e8482301723f?d=identicon)[zeeke](/maintainers/zeeke)

---

Top Contributors

[![zeeke](https://avatars.githubusercontent.com/u/2817917?v=4)](https://github.com/zeeke "zeeke (41 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

phpframeworkdependency-injection

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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