PHPackages                             phossa/phossa-di - 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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. phossa/phossa-di

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

phossa/phossa-di
================

A fast and full-fledged dependency injection library for PHP. It supports auto wiring, container delegation, object decorating, definition provider, definition tags, service scope and more.

1.0.7(10y ago)238MITPHPPHP &gt;=5.4.0CI failing

Since Feb 23Pushed 6y ago1 watchersCompare

[ Source](https://github.com/phossa/phossa-di)[ Packagist](https://packagist.org/packages/phossa/phossa-di)[ Docs](https://github.com/phossa/phossa-di)[ RSS](/packages/phossa-phossa-di/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (7)Dependencies (4)Versions (7)Used By (0)

phossa-di \[ABANDONED\]
=======================

[](#phossa-di-abandoned)

[![Build Status](https://camo.githubusercontent.com/f5c05702f6517af9559fc0a6d1aa1df0d54fe500c5929c11cf3d5c94322f0391/68747470733a2f2f7472617669732d63692e6f72672f70686f7373612f70686f7373612d64692e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/phossa/phossa-di.svg?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/8ad48547624b3ece62f7e2d3386e7c5857ba98ecb163650ff0abec13761296c7/68747470733a2f2f706f7365722e707567782e6f72672f70686f7373612f70686f7373612d64692f762f737461626c65)](https://packagist.org/packages/phossa/phossa-di)[![License](https://camo.githubusercontent.com/75fa88d3df0174bc0e021f5b5646725230150bc23f89cfb0c8bbf202d0a4bfdd/68747470733a2f2f706f7365722e707567782e6f72672f70686f7373612f70686f7373612d64692f6c6963656e7365)](https://packagist.org/packages/phossa/phossa-di)

**See new lib at [phoole/di](https://github.com/phoole/di)**Introduction
------------------------------------------------------------------------

[](#see-new-lib-at-phoolediintroduction)

**Phossa-di** is a *fast*, *feature-rich* and *full-fledged* dependency injection library for PHP. It supports [auto wiring](#auto), [container delegation](#delegate), [object decorating](#decorate), [definition provider](#provider), [definition tagging](#tag), [object scope](#scope) and more.

It requires PHP 5.4 and supports PHP 7.0+, HHVM. It is compliant with [PSR-1](http://www.php-fig.org/psr/psr-1/ "PSR-1: Basic Coding Standard"), [PSR-2](http://www.php-fig.org/psr/psr-2/ "PSR-2: Coding Style Guide"), [PSR-4](http://www.php-fig.org/psr/psr-4/ "PSR-4: Autoloader") and coming [PSR-5](https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md "PSR-5: PHPDoc"), [PSR-11](https://github.com/container-interop/fig-standards/blob/master/proposed/container.md "Container interface").

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

[](#getting-started)

- **Installation**

    Install via the `composer` utility.

    ```
    composer require "phossa/phossa-di=1.*"

    ```

    or add the following lines to your `composer.json`

    ```
    {
        "require": {
          "phossa/phossa-di": "^1.0.6"
        }
    }
    ```
- **Simple usage**

    You might have serveral simple classes like these or third party libraries, and want to make avaiable as services.

    ```
    class MyCache
    {
        private $driver;

        public function __construct(MyCacheDriver $driver)
        {
            $this->driver = $driver;
        }

        // ...
    }

    class MyCacheDriver
    {
        // ...
    }
    ```

    Now do the following,

    ```
    use Phossa\Di\Container;

    $container = new Container();

    // use the 'MyCache' classname as the service id
    if ($container->has('MyCache')) {
        $cache = $container->get('MyCache');
    }
    ```

    With [auto wiring](#auto) is turned on by default, the container will look for the `MyCache` class and resolves its dependency injection automatically when creating the `$cache` instance.
- **Use with definitions**

    Complex situations may need extra configurations. Definition related methods `add()`, `set()`, `map()`, `addMethod()` and `setScope()` etc. can be used to configure services.

    ```
    use Phossa\Di\Container;

    // turn off auto wiring
    $container = (new Container())->auto(false);

    // config service with id, classname and constructor arguments
    $container->add('cache', 'MyCache', [ '@cacheDriver@' ]);

    // add initialization methods
    $container->add('cacheDriver', 'MyCacheDriver')
              ->addMethod('setRoot', [ '%cache.root%' ]);

    // set a parameter which is referenced before
    $container->set('cache.root', '/var/local/tmp');

    // get cache service by its id
    $cache = $container->get('cache');
    ```

    - *Service definitions*

        `Service` is defined using API `add($id, $classOrClosure, array $arguments)`and later can be refered in other definition with service reference `@service_id@`

        ```
        $container = new Container();

        // add the 'cache' service definition
        $container->add('cache', \Phossa\Cache\CachePool::class, ['@cacheDriver@']);

        // add the 'cacheDriver' service definition
        $container->add('cacheDriver', \Phossa\Cache\Driver\FilesystemDriver);

        // get cache service
        $cache = $container->get('cache');
        ```

        Service reference in the format of `@service_id@` can be used anywhere where an object is appropriate, such as in the argument array or construct a pseudo callable,

        ```
        // will resolve this ['@cache@', 'setLogger'] to a real callable
        $container->run(['@cache@', 'setLogger'], ['@logger@']);
        ```
    - *Parameter definitions*

        Parameter can be set with API `set($name, $value)`. Parameter reference is '%parameter.name%'. Parameter reference can point to a string, another parameter or even a service reference.

        ```
        // set system temp directory
        $container->set('system.tmpdir', '/var/tmp');

        // point cache dir to system temp
        $container->set('cache.dir', '%system.tmpdir%');

        // set with array of vales
        $container->set('logger', [
            'driver' => 'Phossa\Logger\Driver\StreamDriver',
            'level'  => 'warning'
        ]);

        // use parameter
        $container->add(
            'cacheDir',
            Phossa\Cache\Driver\Filesystem::class,
            [ '%cache.dir%' ]
        );
        ```
- **Callable instead of class name**

    Callable can be used instead of class name to instantiate a service.

    ```
    // ...
    $container->add('cacheDriver', function() {
        return new \MyCacheDriver();
    });
    ```
- **Definition files**

    Instead of configuring `$container` in the code, you may put your service and parameter definitions into one definition file or several seperated files *(seperating parameter definitions from service definitions will give you the benefit of loading different parameters base on different situations.)*.

    PHP, JSON, XML file formats are supported, and will be detected automatically base on the filename suffixes.

    A service definition file `definition.serv.php`

    ```
