PHPackages                             asgard/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. asgard/container

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

asgard/container
================

v0.3.1(10y ago)03.3k7MITPHPPHP &gt;=5.5.9

Since Sep 9Pushed 8y agoCompare

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

READMEChangelog (4)Dependencies (1)Versions (4)Used By (7)

\#Container

[![Build Status](https://camo.githubusercontent.com/92640dc5df62c8aeae5c1f38d2708c2b103ebb9e1978ad1497bffb2ab5a08e95/68747470733a2f2f7472617669732d63692e6f72672f6173676172647068702f636f6e7461696e65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/asgardphp/container)

The container provides services to the application. In the Asgard framework, the container is often stored in the container variable.

- [Installation](#installation)
- [Usage in the Asgard Framework](#usage-asgard)
- [Usage outside the Asgard Framework](#usage-outside)
- [Registering a service](#registering)
- [Accessing a service](#accessing)
- [Creating a new service instance](#creating)
- [Checking if a service exists](#checking)
- [Removing a service](#removing)
- [ContainerAware Trait](#containeraware)
- [Commands](#commands)

\##Installation **If you are working on an Asgard project you don't need to install this library as it is already part of the standard libraries.**

```
composer require asgard/container 0.*

```

\##Usage in the Asgard Framework

The container is often accessible as a method parameter or through a [ContainerAware](#containeraware) object. You can also use the [singleton](#usage-outside) but it is not recommended.

\##Usage outside the Asgard Framework

```
$container = new \Asgard\Container\Container;
#or
$container = new \Asgard\Container\Container::singleton();

```

\##Registering a service

```
$container->register('cache', function($container, $param) {
	return new \Cache($param);
});
#or
$container['cache'] = new \Cache($param);
#or
$container->set('cache', new \Cache($param));

```

Register a service without persistency:

```
$container->register('cache', function($container, $param) {
	return new \Cache($param);
}, false);

```

$container\['cache'\] will create a new instance every time it is called.

\##Accessing a service

```
$cache = $container->get('cache', [$param]);
#or
$cache = $container['cache'];

```

If you call it multiple times, the container will make sure the same instance is returned every time.

\##Creating a new service instance

```
$cache = $container->make('cache', [$param]);

```

\##Checking if a service exists

```
$container->has('cache');
#or
isset($container['cache']);

```

\##Removing a service

```
$container->remove('cache');
#or
unset($container['cache']);

```

\##ContainerAware Trait

This trait provides two methods:

- setContainer($container)
- getContainer()

and a protected member variable "$container".

To use it, add the following line in a class just after the opening bracket

```
use \Asgard\Container\ContainerAware;

```

\##Commands

\###ListCommand

Show all the services loaded in the application.

Usage:

```
php console services [--defined] [--registered]

```

\--defined: to show where a service was defined

\--registered: to shown where a service was registered

\###Contributing

Please submit all issues and pull requests to the [asgardphp/asgard](http://github.com/asgardphp/asgard) repository.

### License

[](#license)

The Asgard framework is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity50

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

Total

4

Last Release

3656d ago

PHP version history (2 changes)v0.1.0PHP &gt;=5.4.0

v0.3.0PHP &gt;=5.5.9

### Community

Maintainers

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

---

Top Contributors

[![h0gar](https://avatars.githubusercontent.com/u/439138?v=4)](https://github.com/h0gar "h0gar (54 commits)")

### Embed Badge

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

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

###  Alternatives

[thinkcmf/cmf-swoole

Swoole extend for ThinkCMF5.1

371.8k](/packages/thinkcmf-cmf-swoole)

PHPackages © 2026

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