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

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

bayfrontmedia/container
=======================

An easy to use PSR-11 compatible dependency injection container.

v3.0.1(1y ago)01.3k11MITPHPPHP ^8.0

Since Aug 2Pushed 1y ago1 watchersCompare

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

READMEChangelog (9)Dependencies (1)Versions (10)Used By (1)

Container
---------

[](#container)

An easy to use PSR-11 compatible dependency injection container.

- [License](#license)
- [Author](#author)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)

License
-------

[](#license)

This project is open source and available under the [MIT License](LICENSE).

Author
------

[](#author)

[![Bayfront Media](https://camo.githubusercontent.com/0c0163913b2092e97dbf9684970adaf86f2f25871298d3d28b2dff4bf75b2915/68747470733a2f2f63646e312e6f6e62617966726f6e742e636f6d2f62666d2f6272616e642f62666d2d6c6f676f2e737667)](https://camo.githubusercontent.com/0c0163913b2092e97dbf9684970adaf86f2f25871298d3d28b2dff4bf75b2915/68747470733a2f2f63646e312e6f6e62617966726f6e742e636f6d2f62666d2f6272616e642f62666d2d6c6f676f2e737667)

- [Bayfront Media homepage](https://www.bayfrontmedia.com?utm_source=github&utm_medium=direct)
- [Bayfront Media GitHub](https://github.com/bayfrontmedia)

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

[](#requirements)

- PHP `^8.0` (Tested up to `8.4`)

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

[](#installation)

```
composer require bayfrontmedia/container

```

Usage
-----

[](#usage)

### Start using the container

[](#start-using-the-container)

```
use Bayfront\Container;

$container = new Container();

```

### Public methods

[](#public-methods)

- [set](#set)
- [getEntries](#getentries)
- [get](#get)
- [make](#make)
- [has](#has)
- [remove](#remove)
- [setAlias](#setalias)
- [getAliases](#getaliases)
- [hasAlias](#hasalias)
- [removeAlias](#removealias)

---

### set

[](#set)

**Description:**

Set an entry into the container.

Anonymous functions (closures) are called on the first `get()`.

**Parameters:**

- `$id` (string)
- `$value` (mixed)
- `$overwrite = false` (bool): If false, a `ContainerException` is thrown if an entry with the same ID already exists. Otherwise, it is overwritten.

**Returns:**

- (void)

**Throws:**

- `Bayfront\Container\ContainerException`

**Example:**

Any type of value can be set in the container.

Services with dependencies can be set using an anonymous function (a `\Closure`) which returns an instance of the class.

The first time a service is requested from the container using `get()`, the anonymous function is called and the result is saved. On subsequent calls, the same ID always returns the same result.

The anonymous function always calls the container instance as the first argument. This allows you to reference other items from the container, if needed. If you do not need access to the container, the parameter may be omitted from the function signature.

```
// Set a service with no dependencies

$container->set('Fully\Namespaced\ClassName', function () {
    return new ClassName();
});

// Set a service with dependencies

$container->set('Fully\Namespaced\ClassName', function (ContainerInterface $container) {
    $dependency = $container->get('Fully\Namespaced\Dependency');
    return new ClassName($dependency);
});

// Any type of value can be set, then used as a parameter

$container->set('classname_config', [
    // Config array
]);

$container->set('Fully\Namespaced\ClassName', function (ContainerInterface $container) {
    $config = $container->get('classname_config');
    return new ClassName($config);
});

// Preexisting class instances can be set without using an anonymous function

$class = new ClassName();
$container->set('ClassName', $class);
```

---

### getEntries

[](#getentries)

**Description:**

Returns an array of all ID's existing in the container.

**Parameters:**

- None.

**Returns:**

- (array)

**Example:**

```
$entries = $container->getEntries();
```

---

### get

[](#get)

**Description:**

Get an entry from the container by its ID or alias.

**Parameters:**

- `$id` (string)

**Returns:**

- (mixed)

**Throws:**

- `Bayfront\Container\NotFoundException`

**Example:**

```
$service = $container->get('Fully\Namespaced\ClassName');
```

---

### make

[](#make)

**Description:**

Makes and returns a new class instance, automatically injecting dependencies which exist in the container.

**Parameters:**

- `$class` (string)
- `$params = []` (array): Additional parameters to pass to the class constructor.

**Returns:**

- (mixed)

**Throws:**

- `Bayfront\Container\ContainerException`
- `Bayfront\Container\NotFoundException`

**Example:**

```
class ClassName {

    protected $service;
    protected $config;

    public function __construct(AnotherService $service, array $config)
    {
        $this->service = $service;
        $this->config = $config;
    }

}

$instance = $container->make('Fully\Namespaced\ClassName', [
    'config' => []
]);
```

---

### has

[](#has)

**Description:**

Does entry or alias exist in the container? (ie: Can an entry be resolved using `get()` with this ID?)

**Parameters:**

- `$id` (string): ID or alias

**Returns:**

- (bool)

**Example:**

```
if ($container->has('Fully\Namespaced\ClassName')) {
    // Do something
}
```

---

### remove

[](#remove)

**Description:**

Remove entry from container, if existing.

**Parameters:**

- `$id` (string)

**Returns:**

- (void)

**Example:**

```
$container->remove('Fully\Namespaced\ClassName');
```

---

### setAlias

[](#setalias)

**Description:**

Set an alias for a given ID.

**Parameters:**

- `$alias` (string)
- `$id` (string)
- `$overwrite = false` (bool): If false, a `ContainerException` is thrown if an alias with the same name already exists. Otherwise, it will be overwritten.

**Returns:**

- (void)

**Throws:**

- `Bayfront\Container\ContainerException`

**Example:**

```
$container->setAlias('alias', 'Fully\Namespaced\ClassName');
```

One benefit of aliases is that they allow you to retrieve entries from the container in a concise, easy to remember manner. In addition, aliases allow you to bind an interface to an implementation.

For example:

```
$container->setAlias('Fully\Namespaced\Implementation', 'Fully\Namespaced\Interface');
```

Now, whenever a class requires an implementation of `Fully\Namespaced\Interface`, an instance of `Fully\Namespaced\Implementation` will be returned, if existing in the container.

---

### getAliases

[](#getaliases)

**Description:**

Returns an array of all existing aliases.

**Parameters:**

- None.

**Returns:**

- (array)

**Example:**

```
$aliases = $container->getAliases();
```

---

### hasAlias

[](#hasalias)

**Description:**

Does alias exist?

**Parameters:**

- `$alias` (string)

**Returns:**

- (bool)

**Example:**

```
if ($container->hasAlias('alias')) {
    // Do something
}
```

---

### removeAlias

[](#removealias)

**Description:**

Remove alias.

**Parameters:**

- `$alias` (string)

**Returns:**

- (void)

**Example:**

```
$container->removeAlias('alias');
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance40

Moderate activity, may be stable

Popularity15

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity65

Established project with proven stability

 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 ~200 days

Recently: every ~391 days

Total

9

Last Release

510d ago

Major Versions

1.1.3 → v2.0.02023-01-21

v2.0.1 → v3.0.02023-01-26

PHP version history (3 changes)1.0.0PHP &gt;=7.2.0

v2.0.0PHP ^7.2.0|^8.0

v3.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5ad62c8d0e69358fd63b16fdaa71d5359231cd0cf660bbc3419071dc705c63a8?d=identicon)[bayfrontmedia](/maintainers/bayfrontmedia)

---

Top Contributors

[![robinsonjohn](https://avatars.githubusercontent.com/u/24327848?v=4)](https://github.com/robinsonjohn "robinsonjohn (24 commits)")

---

Tags

containerdependencydiinjectionphppsr-11phpcontainerPSR-11dependency-injectiondialias

### Embed Badge

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

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

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k48.9M994](/packages/php-di-php-di)

PHPackages © 2026

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