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

ActiveLibrary[Framework](/categories/framework)

opxcore/container
=================

The OpxCore dependency injection container.

1.0.3(5y ago)1135↓66.7%2MITPHPPHP ^7.4CI failing

Since Jan 12Pushed 5y agoCompare

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

READMEChangelog (7)Dependencies (2)Versions (8)Used By (2)

Dependency injection container
==============================

[](#dependency-injection-container)

[![Build Status](https://camo.githubusercontent.com/05dda8323b09aacd325bd9109f4d962c3371f159d35e6bc271f21c116731e27f/68747470733a2f2f7777772e7472617669732d63692e636f6d2f6f7078636f72652f636f6e7461696e65722e7376673f6272616e63683d6d6173746572)](https://www.travis-ci.com/opxcore/container)[![Coverage Status](https://camo.githubusercontent.com/26b6319b9ef03654e3ea0856ab925e0811bbcb352730822df09d2a9ad8edab79/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6f7078636f72652f636f6e7461696e65722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/opxcore/container?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/35b34a7da2cb15054bbbc6321c7768c482fe3bb6a637b57fba47bafd8deb67d0/68747470733a2f2f706f7365722e707567782e6f72672f6f7078636f72652f636f6e7461696e65722f762f737461626c65)](https://packagist.org/packages/opxcore/container)[![Total Downloads](https://camo.githubusercontent.com/ad468bad35b9bca715dd6ecc193ad88ca6e6017cc55a1c93dde7b7923e2da3b5/68747470733a2f2f706f7365722e707567782e6f72672f6f7078636f72652f636f6e7461696e65722f646f776e6c6f616473)](https://packagist.org/packages/opxcore/container)[![License](https://camo.githubusercontent.com/16e2ea5cbd496949a9e5637fd50b78de0a0301b352f1eede242b6899ff70ab1f/68747470733a2f2f706f7365722e707567782e6f72672f6f7078636f72652f636f6e7461696e65722f6c6963656e7365)](https://packagist.org/packages/opxcore/container)

Introduction
============

[](#introduction)

The dependency injection container is a powerful tool for managing class dependencies and performing dependency injection. Class dependencies are "injected" into the class via the constructor and resolved by the container.

### Example:

[](#example)

```
class Controller
{
    protected Repository $repository;

    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }
}
```

Calling `$container->make(Controller::class)` would be equal to `new Controller(new Repository)`. This amazing feature resolves all dependency injections automatically with *zero-config*. For this example if `Repository` have its own dependency, it will be resolved the same.

Installing
==========

[](#installing)

`composer require opxcore/container`

Creating
========

[](#creating)

You can create a container several ways:

```
$container = Container::setContainer(new Container);
```

or

```
$container = Container::getContainer();
```

In all of this cases `Container::getContainer()` will always return the same container instance.

If you want to create and handle a container (or several containers) by yourself just use `$container = new Container` and handle this container instance as you want.

Registering a binding with the container
========================================

[](#registering-a-binding-with-the-container)

Basic binding to container looks like:

```
$container->bind($abstract, $concrete, $parameters);
```

`$abstract` is a string containing class name or shorthand for a name to be resolved.

`$concrete` is a string containing class name or `callable` returning object for a name to be resolved to. Container instance and parameters will be passed to callable during resolving.

`$parameters` is an array or callable returning array with parameters used for resolving (see below). Default value is `null` means no parameters will be bound.

Examples:
---------

[](#examples)

### Simple usage

[](#simple-usage)

```
$container->bind('logger', Logger::class);

// New instance of Logger with resolved dependencies will be retuened.
$logger = $container->make('logger');
```

### Binding interfaces to its realizations

[](#binding-interfaces-to-its-realizations)

We have `Controller` class what depends on some `RepositoryInterface` and `FileRepository` implementing it:

```
class Controller
{
    protected RepositoryInterface $repository;

    public function __construct(RepositoryInterface $repository)
    {
        $this->repository = $repository;
    }
}
```

```
class FileRepository implements RepositoryInterface
{
    protected string $path;

    public function __construct(string $path)
    {
        $this->path = $path;
    }
}
```

Now we bind `FileRepository::class` to `RepositoryInterface::class` so when some class depends on `RepositoryInterface`it will be resolved to `FileRepository` and `path` argument will be passed into with specified value.

```
$container->bind(RepositoryInterface::class, FileRepository::class, ['path'=>'/data/storage']);

$controller = $container->make(Controller::class);
```

Binding parameters
------------------

[](#binding-parameters)

You can bind parameters into the container fo resolving. It can be used as primitives binding or class binding:

```
// When FileRepository dependencies would be resolving, given value would be passed to `path` attribute.
$container->bindParameters(FileRepository::class, ['path' => '/data/storage']);

// When Controller would be resolved a FileRepository would be given as RepositoryInterface dependency.
$container->bindParameters(Controller::class, [RepositoryInterface::class => FileRepository::class]);
```

Singleton
---------

[](#singleton)

The singleton method binds a class or interface into the container that should be resolved once. First time it will be resolved and stored in the container, so other times the same object instance will be returned on subsequent calls into the container.

```
$container->singleton(RepositoryInterface::class, FileRepository::class, ['path'=>'/data/storage']);

// Each time when RepositoryInterface needs to be resolved
// the same instance of FileRepository would be given.
$repository = $container->make(RepositoryInterface::class);
```

Alias
-----

[](#alias)

Alias is another name for resolving.

```
$container->singleton(RepositoryInterface::class, FileRepository::class, ['path'=>'/data/storage']);
$container->alias(RepositoryInterface::class, 'repo');

// This would return FileRepository instance.
$container->make('repo');
```

Instance
--------

[](#instance)

You can register any object or value into the container.

```
$container->instance('path', '/data/storage');
// '/data/storage' would be returned
$container->make('path');

$container->instance(RepositoryInterface::class, new FileRepository('/data/storage'));
// FileRepository would be returned
$container->make(RepositoryInterface::class);
```

Resolving
=========

[](#resolving)

When resolving some name, you can pass parameters to `make()` function to attach it for resolving subject.

```
$container->bind(RepositoryInterface::class, FileRepository::class);
$container->make(RepositoryInterface::class, ['path'=>'/data/storage']);
```

Sequence of making subject
--------------------------

[](#sequence-of-making-subject)

When resolving some subject, the container checks for registered aliases first, checks for registered instances and resolves using reflections.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 88% 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 ~118 days

Recently: every ~170 days

Total

7

Last Release

1969d ago

Major Versions

0.3.2 → 1.0.02020-12-16

PHP version history (2 changes)0.3PHP ^7.1.3

1.0.0PHP ^7.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/46634185?v=4)[opxcore](/maintainers/opxcore)[@opxcore](https://github.com/opxcore)

---

Top Contributors

[![lozovoyv](https://avatars.githubusercontent.com/u/21274927?v=4)](https://github.com/lozovoyv "lozovoyv (22 commits)")[![opxcore](https://avatars.githubusercontent.com/u/46634185?v=4)](https://github.com/opxcore "opxcore (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/opxcore-container/health.svg)](https://phpackages.com/packages/opxcore-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)
