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

ActivePackage[Framework](/categories/framework)

apex/container
==============

Light-weight DI container

2.0.9(2y ago)61.2k8MITPHPPHP &gt;=8.1

Since Jan 29Pushed 2y ago1 watchersCompare

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

READMEChangelogDependencies (3)Versions (25)Used By (8)

Apex Container
==============

[](#apex-container)

A lightweight, straight forward dependency injection container that simply works, and works well. Supports config file and all standard injection methods -- constructor, setter, annotation, plus also attributes. Also includes a `Di` wrapper class that allows container methods to be accessed statically for greater simplicity and efficiency.

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

[](#installation)

Install via Composer with:

> `composer require apex/container`

Basic Usage
-----------

[](#basic-usage)

Supports the standard container methods -- `has, get, set, make, call`,

```
use Apex\Container\Container;

$cntr = new Container('/path/to/definitions.php');

// Set and get
$cntr->set('full_name', 'John Doe');
// Get service
$logger = $cntr->get(LoggerInterface::class);
$logger->info("Here I am");

// Make and set
$user = $cntr->makeset(User::class, ['id' => 311]);

// Call via method setter injection
$cntr->call([$user, 'updateStatus'], ['status' => 'inactive']);
```

Constructor
-----------

[](#constructor)

Constructor takes the following parameters, none of which are required.

ParameterTypeDescription`$config_file`stringLocation of the definitions file which defines starting items and services. Should be a PHP file that returns an associative array. Please see the [Definitions File](https://github.com/apexpl/container/blob/master/docs/definitions.md) page for details. If defind in the constructor, will be automatically loaded.`$use_autowiring`boolWhether or not to enable auto-wiring. If true, the `use` declarations of all files will be loaded, and checked for injection parameters. Defaults to true.`$use_attributes`boolIf true, will scan attributes in all files for injection properties. Please see the [Attributes Injection](https://github.com/apexpl/container/blob/master/docs/attributes.md) page for details. Defaults to false.`$use_annotations`boolIf true, will scan the properties and doc blocks of all files for injected properties. Please see the [Annotation Injection](https://github.com/apexpl/container/blob/master/docs/annotations.md) page for details. Defaults to false.Mark Items as Services
----------------------

[](#mark-items-as-services)

You must explicitly mark necessary items as services, meaning the first time they are retrived from the container, they will be automatically instantiated with the new object instance set in the container for future use. Retrieving instantiable items from the container that are not marked as services will simply return the class name / closure, and will not instantiate the object.

You may easily mark items as services with the `markItemAsService()` method, for example:

```
use Apex\Container\Container;
use Apex\Psr\Log\LoggerInterface;

$cntr = new Container();
$cntr->markItemAsService(LoggerInterface::class);
$cntr->markItemAsService('debugger');
```

Methods
-------

[](#methods)

The following base methods are supported, as per all containers.

MethodDescription`has(string $name):bool`Check whether or not item is available in container.`get(string $name):mixed`Gets an item from the container. If does not already exist, will try to make and set the item. Returns null on non-existent item.`set(string $name, mixed $item):void`Sets an item in the container.`make(string $name, array $params = []):mixed`Creates new instance of the class with provided parameters.`makeset(string $name, array $params = []):mixed`Creates new instance of the class with provided parameters, and also sets class name into container for future use.`call(callablearray $callable, array $params = \[\]):mixed``buildContainer(string $config_file):void`Build container with the specified definitions file. Useful if you didn't pass a definitions file to the constructor, or if you're using the `Di` class to access the container statically.Singleton Di Class
------------------

[](#singleton-di-class)

This package includes the `Di` class which acts as a singleton and allows container methods to be accessed statically, instead of passing the container object from class to class, method to method. This is done to provide efficiency and simplicity, and also assumes you're only managing one DI container per-request.

Modify the properties within the `/src/Di.php` file as they are used as the constructor parameters for the container. Example usage is below.

```
namespace myApp;

use Apex\Container\Di;
use myApp\Users\User;

// Get logger
$logger = Di::get(LoggerInterface::Class);

// Make user object
Di::make(User::class, ['id' => 52]);

// Set and get variable
Di::set('full_name', 'John Doe');
$name = Di::get('full_name');   // John Doe
```

Follow Apex
-----------

[](#follow-apex)

Loads of good things coming in the near future including new quality open source packages, more advanced articles / tutorials that go over down to earth useful topics, et al. Stay informed by joining the [mailing list](https://apexpl.io/) on our web site, or follow along on Twitter at [@mdizak1](https://twitter.com/mdizak1).

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 66.7% 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 ~46 days

Recently: every ~90 days

Total

24

Last Release

849d ago

Major Versions

0.3.7 → 2.02022-01-30

PHP version history (2 changes)0.1PHP &gt;=8.0.0

2.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/4fe486d2ed7db571c0519bb0d52b08b5e953a911936e87331e736a381ef96f29?d=identicon)[apex](/maintainers/apex)

---

Top Contributors

[![mdizak](https://avatars.githubusercontent.com/u/59886259?v=4)](https://github.com/mdizak "mdizak (18 commits)")[![apexpl](https://avatars.githubusercontent.com/u/56146521?v=4)](https://github.com/apexpl "apexpl (9 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[silverstripe/framework

The SilverStripe framework

7213.5M2.5k](/packages/silverstripe-framework)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6939.5M341](/packages/drupal-core-recommended)[cakephp/core

CakePHP Framework Core classes

6026.8M39](/packages/cakephp-core)[contao/core-bundle

Contao Open Source CMS

1231.6M2.3k](/packages/contao-core-bundle)[neos/flow

Flow Application Framework

862.0M449](/packages/neos-flow)

PHPackages © 2026

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