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

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

baublet/dependency-container
============================

A simple, PSR-4 compliant dependency container.

0.0.0(9y ago)116MITPHPPHP &gt;=5.4.0

Since Apr 17Pushed 9y agoCompare

[ Source](https://github.com/baublet/DependencyContainer)[ Packagist](https://packagist.org/packages/baublet/dependency-container)[ RSS](/packages/baublet-dependency-container/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

DependencyContainer
===================

[](#dependencycontainer)

[![Build Status](https://camo.githubusercontent.com/61aa520c854049545081551de519d94fa8b4cbd0931e2f1c3e1613cb3030987a/68747470733a2f2f7472617669732d63692e6f72672f626175626c65742f446570656e64656e6379436f6e7461696e65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/baublet/DependencyContainer)

For simple dependency injection

Why?
----

[](#why)

Sometimes you need dependencies across your application, plugin, or some other system that doesn't itself have a way to handle it (e.g., WordPress). So include this project as a submodule and inject dependencies into your classes at your leisure. Use a container when you don't want to pollute your constructors with a ton of dependencies.

This container also only loads dependencies when a class tries to access the dependency in question. That's why all of our injections are declared as functions. For more information on this, see below.

It's PSR compliant, implementing the [(proposed) Container interfaces](https://github.com/php-fig/fig-standards/blob/master/proposed/container.md) for the API. If the API changes in the interfaces, I'll update these to match.

This means that the API for this is subject to change, so please read any documentation before upgrading.

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

[](#installation)

Add `baublet/dependency-container` to the required packages portion of your `composer.json` file, or, from the command line:

```
composer require baublet/dependency-container
```

Example
-------

[](#example)

I've tried to make the API fairly simple. First, require the container module and declare a new container.

```
use baublet\DependencyContainer\DependencyContainer;

$dependencies = new DependencyContainer();
```

Dependencies are declared as functions, so that you can ensure your dependencies are only loaded when called. This allows you to use this container as a registry of dependencies in a particular module that, when called, may or may not use all of its dependencies in a given call.

To declare a dependency:

```
$constructor = "This will be passed to the function as $args";

$dependencies->set("my-dependency", function($args) {
    require_once("some/big/dependency/class.php");
    return new SomeBigDependencyClass($args);
}, $constructor);
```

In the above code, `some/big/dependency/class.php` is only required and `SomeBigDependencyClass` is only initialized when you call `$dependencies->get("my-dependency");`.

Testing
-------

[](#testing)

This code is tested using Travis CI and plain PHP assertions. No testing framework required. Just hop into the test directory and run the test:

```
php test.php

```

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

3361d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/661f9265cd299dabf4c8bc82f43cb6682584a32c67d7bae41d2b497b169d7ff1?d=identicon)[baublet](/maintainers/baublet)

---

Tags

dependenciesdependencycontainerservice-locatordependencydependency-injectiondependenciesdependency managementDependency-Containerservice locator

### Embed Badge

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

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

###  Alternatives

[pimple/pimple

Pimple, a simple Dependency Injection Container

2.7k133.2M1.4k](/packages/pimple-pimple)[php-di/php-di

The dependency injection container for humans

2.8k53.2M1.2k](/packages/php-di-php-di)[league/container

A fast and intuitive dependency injection container.

86892.2M397](/packages/league-container)[laminas/laminas-servicemanager

Factory-Driven Dependency Injection Container

15857.5M782](/packages/laminas-laminas-servicemanager)[acclimate/container

Provides adapters for various third-party service containers.

219394.9k15](/packages/acclimate-container)[capsule/di

A PSR-11 compliant autowiring dependency injection container.

2859.2k2](/packages/capsule-di)

PHPackages © 2026

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