PHPackages                             encorephp/container - 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. encorephp/container

ActiveLibrary[Framework](/categories/framework)

encorephp/container
===================

Dependency injection container with service providers and proxies.

1297PHP

Since Mar 9Pushed 11y ago1 watchersCompare

[ Source](https://github.com/chrisnharvey/container)[ Packagist](https://packagist.org/packages/encorephp/container)[ RSS](/packages/encorephp-container/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

EncorePHP Container
===================

[](#encorephp-container)

[![Latest Version](https://camo.githubusercontent.com/533d513589d146232bf2f6375ed5a6568dbfa60aca98545eeb3b04e0f1307887/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f656e636f72657068702f636f6e7461696e65722e7376673f7374796c653d666c61742d737175617265)](https://github.com/encorephp/container/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/3dfcbf0dc7c38a607e8ac2fed6926a2519b25c5529172053a200a7598b1baa07/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f656e636f72657068702f636f6e7461696e65722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/encorephp/container)[![Coverage Status](https://camo.githubusercontent.com/9c48ad344da5184f9b2832446a7242916c2757298487e87ec9cc868f48374fd9/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f656e636f72657068702f636f6e7461696e65722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/encorephp/container/code-structure)[![Quality Score](https://camo.githubusercontent.com/c5553e265483fedb9b403015e91ec4f49ed7f192a0284fe98acd944e0b569455/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f656e636f72657068702f636f6e7461696e65722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/encorephp/container)[![Total Downloads](https://camo.githubusercontent.com/0c8198e7cbfedf8ce3a3a59a8f97bb01dba09feb19cc873b0823ea44273d7072/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656e636f72657068702f636f6e7461696e65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/encorephp/container)

The EncorePHP Container library provides a fast and powerful Dependency Injection Container for your application.

As well as a dependency injection container, we also allow the use of service providers and proxies.

Install
-------

[](#install)

Via Composer

```
$ composer require encorephp/container
```

Usage
-----

[](#usage)

### Get a Container Object

[](#get-a-container-object)

```
require 'vendor/autoload.php';

$container = new Encore\Container\Container;
```

### Bind a concrete class to an interface

[](#bind-a-concrete-class-to-an-interface)

```
$container->bind('\Foo\Bar\BazInterface', '\Foo\Bar\Baz');
```

### Automatic Dependency Resolution

[](#automatic-dependency-resolution)

The Container is able to recursively resolve objects and their dependencies by inspecting the type hints on an object's constructor.

```
namespace Foo\Bar;

class Baz
{
  public function __construct(Qux $qux, Corge $corge)
  {
    $this->qux = $qux;
    $this->corge = $corge;
  }

  public function setQuux(Quux $quux)
  {
    $this->quux = $quux;
  }
}

$container->resolve('\Foo\Bar\Baz');
```

### Defining Arguments

[](#defining-arguments)

Alternatively, you can specify what to inject into the class upon instantiation.

#### Define Constructor Args

[](#define-constructor-args)

```
$container->bind('\Foo\Bar\Baz')->addArgs(array('\Foo\Bar\Quux', '\Foo\Bar\Corge'));

$container->resolve('\Foo\Bar\Baz');
```

#### Define Methods to Call with Args

[](#define-methods-to-call-with-args)

```
$container->bind('\Foo\Bar\Baz')->withMethod('setQuux', array('\Foo\Bar\Quux'));

$container->resolve('\Foo\Bar\Baz');
```

### Resolve Inherited Dependencies

[](#resolve-inherited-dependencies)

Another great feature of the the container is the ability to resolve the dependencies of inherited classes and interfaces. For example, you could bind an interface into the container which requires a dependency injected via a method, and all classes that implement that interface will also have that dependency injected automatically.

```
$container->bind('\Foo\Bar\BazInterface')->withMethod('setQuux', ['\Foo\Bar\Quux']);

$container->bind('\Foo\Bar\Baz');

$container->resolve('\Foo\Bar\Baz');
```

### Child Containers and Scope Resolution

[](#child-containers-and-scope-resolution)

A great feature of the container is it's ability to provide child containers with a separate resolution scope to that of it's parent container. If you bind a concrete class to an interface within one container, you can re-bind it in the child container, without fear of overwriting the original binding in the parent container.

#### Creating a Child Container

[](#creating-a-child-container)

There are two ways to create a child container.

```
$child = $continer->createChild();

// OR

$child = new Container($container);
```

#### Using a Child Container

[](#using-a-child-container)

The primary benefit of using child containers is scope-specific resolution.

```
$container->bind('FooInterface', 'Foo');

// Assuming class Bar has a FooInterface dependency.
// This would use the Foo implementation.
$bar = $container->resolve('Bar');

// ...
$child = $container->createChild();
$child->bind('FooInterface', 'Baz');

// And this would use the Baz implementation.
$bar = $child->resolve('Bar');
```

Testing
-------

[](#testing)

```
$ phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

The EncorePHP Container is based onthe deprecated [league/di](https:/github.com/thephpleague/di)package.

- [Chris Harvey](https://github.com/chrisnharvey)
- [Don Gilbert](https://github.com/dongilbert)
- [Phil Bennett](https://twitter.com/philipobenito)
- [All Contributors](../../contributors)
- [Original Contributors](https://github.com/thephpleague/di/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/424e68b8215f79181064984c07fbc268b9b9a5f0ea503a298e76e05a9c70d4d4?d=identicon)[chrisnharvey](/maintainers/chrisnharvey)

---

Top Contributors

[![chrisnharvey](https://avatars.githubusercontent.com/u/619298?v=4)](https://github.com/chrisnharvey "chrisnharvey (71 commits)")

### Embed Badge

![Health badge](/badges/encorephp-container/health.svg)

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

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

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