PHPackages                             modethirteen/opencontainer - 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. modethirteen/opencontainer

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

modethirteen/opencontainer
==========================

A dependency injection container for PHP, please enjoy responsibly

2.1.0(5y ago)03.2k1[1 issues](https://github.com/modethirteen/OpenContainer/issues)Apache-2.0PHPPHP ~7.4.12CI failing

Since Nov 22Pushed 4y ago12 watchersCompare

[ Source](https://github.com/modethirteen/OpenContainer)[ Packagist](https://packagist.org/packages/modethirteen/opencontainer)[ RSS](/packages/modethirteen-opencontainer/feed)WikiDiscussions main Synced 1w ago

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

OpenContainer
=============

[](#opencontainer)

A dependency injection container for PHP, please enjoy responsibly.

[![github.com](https://github.com/modethirteen/OpenContainer/workflows/build/badge.svg)](https://github.com/modethirteen/OpenContainer/actions?query=workflow%3Abuild)[![codecov.io](https://camo.githubusercontent.com/c7775f9d7ee493d69f0ee58d6cbf93bfc494b09704fa0213ee2ccaa34d9d31ed/68747470733a2f2f636f6465636f762e696f2f6769746875622f6d6f6465746869727465656e2f4f70656e436f6e7461696e65722f636f7665726167652e7376673f6272616e63683d6d61696e)](https://codecov.io/github/modethirteen/OpenContainer?branch=main)[![Latest Stable Version](https://camo.githubusercontent.com/184a06617a2549e259563717059c011b82bc6a4bc72dab1c0acf49500dea20b8/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6465746869727465656e2f6f70656e636f6e7461696e65722f76657273696f6e2e737667)](https://packagist.org/packages/modethirteen/opencontainer)[![Latest Unstable Version](https://camo.githubusercontent.com/7126ea99394f40cb0a667aff45b37e0d0c469ba2b05485a805df64ceeb163880/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6465746869727465656e2f6f70656e636f6e7461696e65722f762f756e737461626c65)](https://packagist.org/packages/modethirteen/opencontainer)

About
-----

[](#about)

OpenContainer was created as an attempt to leverage strict type checking available in full-featured PHP development environments, such as JetBrains PHPStorm, or the native strict type checking of PHP 7+, when managing dependencies from a centralized container. In addition, it contains some experiments with reflection and [proxies](https://github.com/Ocramius/ProxyManager) in order to avoid problems when circular dependencies are introduced in the container's dependency chain.

In addition to exposing dependencies as type-checked class properties, OpenContainer is also compatible with the [PSR-11 Container Interface](https://www.php-fig.org/psr/psr-11), for interoperability with several PHP frameworks.

Requirements
------------

[](#requirements)

- PHP 7.4 (main, 2.x)

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

[](#installation)

Use [Composer](https://getcomposer.org/). There are two ways to add OpenContainer to your project.

From the composer CLI:

```
./composer.phar require modethirteen/opencontainer
```

Or add modethirteen/opencontainer to your project's composer.json:

```
{
    "require": {
        "modethirteen/opencontainer": "dev-main"
    }
}
```

`dev-main` is the main development branch. If you are using OpenContainer in a production environment, it is advised that you use a stable release.

Assuming you have setup Composer's autoloader, OpenContainer can be found in the `modethirteen\OpenContainer\` namespace.

Adding OpenContainer to your application
----------------------------------------

[](#adding-opencontainer-to-your-application)

Simply instantiate OpenContainer and you're ready to go.

```
$container = new OpenContainer();
```

Injectable Class
----------------

[](#injectable-class)

An injectable class is instantiated by injecting the container at construction time. In this example, Foo, Bar, and Baz are all types registered in the container. If a registered type in the container requires Baz's method doSomething(), Baz must first pull it's dependencies, Foo and Bar, from the container (and furthermore, their dependencies, creating a dependency tree).

```
class Baz {

  private Foo $foo;
  private Bar $bar;

  public function __construct(IContainer $container) {
    $this->foo = $container->Foo;
    $this->bar = $container->Bar;
  }

  public function doSomething() : string {
    return $this->foo->myMethod();
  }
}
```

Registering Types
-----------------

[](#registering-types)

Registering a type requires a symbol to identify the type when fetching it's instantiated instance from the container, and the fully qualified class name to build.

```
/**
 * setup the type as a virtual property so that IDE's that support type checking can take advantage
 *
 * @property Foo $Foo
 */
class MyContainer extends OpenContainer {
}

$container = new MyContainer();
$container->registerType('Foo', Foo::class);

// type checks will infer this object to be an instance of Foo
$instance = $container->Foo;
```

Registering Instances
---------------------

[](#registering-instances)

Registering an instance requires a symbol to identify the instance when fetching from the container, and the already-created instance itself. Registering an instance is useful when the type's constructor cannot meet the requirements of an injectable class.

```
/**
 * setup the type as a virtual property so that IDE's that support type checking can take advantage
 *
 * @property Bar $Bar
 */
class MyContainer extends OpenContainer {
}

$container = new MyContainer();
$container->registerInstance('Bar', new Bar($dependency, $outside, $of, $container));

// type checks will infer this object to be an instance of Bar
$instance = $container->Bar;
```

Registering Builders
--------------------

[](#registering-builders)

Registering a builder requires a symbol to identify the instance when fetching it from the container, and a closure function to execute the first time it is fetched. Registering a builder is useful if there are specialized steps that must be taken before the instance is created.

```
/**
 * setup the type as a virtual property so that IDE's that support type checking can take advantage
 *
 * @property Qux $Qux
 */
class MyContainer extends OpenContainer {
}

$container = new MyContainer();
$container->registerBuilder('Qux', function(MyContainer $container) : Qux {

  // builder functions only have one argument, access to the container itself
  return new Qux($container, $some, $other, $dependency);
});

// type checks will infer this object to be an instance of Qux
$instance = $container->Qux;
```

Deferred Container
------------------

[](#deferred-container)

A deferred container attempts to use reflection and class proxies to avoid circular dependencies. A deferred container returns proxies, which are not materialized until a method or property is accessed on the proxy. This behavior is useful as without it, every dependency in the tree is instantiated when the root dependency is first instantiated (whether those downstream dependencies will be eventually used or not). Without deferring dependency instantiation until those dependencies are actually needed, any circular dependency returns a null value, an endless nested function loop, or raises an *Undefined Property* warning if they can't be resolved.

```
$container = (new OpenContainer)->toDeferredContainer();

// all methods on a deferred container are identical to a non-deferred container
$container->registerType('Plugh', Plugh::class);
$container->registerInstance('Plugh', new Plugh());

// closure function style builders require a return type, otherwise an OpenContainerCannotBuildDeferredInstanceException will be thrown
$container->registerBuilder('Plugh', function(IContainer $container) : Plugh { ... });
$container->Plugh;
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity55

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.

###  Release Activity

Cadence

Every ~19 days

Total

2

Last Release

1984d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2bfdbec0ff13b1d7836c00d751876485508071cf26e6dcc2bae7fb7333b6f40f?d=identicon)[modethirteen](/maintainers/modethirteen)

---

Top Contributors

[![modethirteen](https://avatars.githubusercontent.com/u/45862?v=4)](https://github.com/modethirteen "modethirteen (41 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/modethirteen-opencontainer/health.svg)

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

###  Alternatives

[illuminate/contracts

The Illuminate Contracts package.

705122.9M10.1k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31178.1M2.0k](/packages/illuminate-container)[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)[symfony/object-mapper

Provides a way to map an object to another object

34885.7k18](/packages/symfony-object-mapper)

PHPackages © 2026

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