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

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

mooti/container
===============

simple service container

0.0.1(9y ago)016Apache-2.0PHPPHP &gt;=5.5.9

Since Aug 31Pushed 9y ago1 watchersCompare

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

READMEChangelogDependencies (4)Versions (2)Used By (0)

Mooti Container
===============

[](#mooti-container)

[![Build Status](https://camo.githubusercontent.com/70aa4ba5f8712f986a84c640cd2d514f53ada053bd7e4181689f27a469cccadf/68747470733a2f2f7472617669732d63692e6f72672f6d6f6f74692f636f6e7461696e65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mooti/container)[![Coverage Status](https://camo.githubusercontent.com/bcbfc714d11aa2794c5ae8d8da7cd8cb098b1f3e7d9b7cbc2000ecd9de5b2556/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d6f6f74692f636f6e7461696e65722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/mooti/container?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/cc1e9e57178ec029715d46678de9e059a2ccef9557b9657b13b5691152c933e3/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6f74692f636f6e7461696e65722f762f737461626c65)](https://packagist.org/packages/mooti/container)[![Total Downloads](https://camo.githubusercontent.com/69b9157ac84a0d11188e3a771193adc60c5364a73fe6538ee085e73dfe5ab82c/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6f74692f636f6e7461696e65722f646f776e6c6f616473)](https://packagist.org/packages/mooti/container)[![Latest Unstable Version](https://camo.githubusercontent.com/5543ea49b0cc9f5ca5d6c308f39144e28f669e15c57affc984a3f3e61415a2b7/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6f74692f636f6e7461696e65722f762f756e737461626c65)](https://packagist.org/packages/mooti/container)[![License](https://camo.githubusercontent.com/475bf1a0f9d3d1a1a3112ac7e8759b349138771a311e97cccab13237cd9917a0/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6f74692f636f6e7461696e65722f6c6963656e7365)](https://packagist.org/packages/mooti/container)

A simple service container written in php

### Installation

[](#installation)

You can install this through packagist

```
$ composer require mooti/container

```

### Run the tests

[](#run-the-tests)

If you would like to run the tests. Use the following:

```
$ ./vendor/bin/phpunit

```

### Usage

[](#usage)

Create the container and add a service

```
use Mooti\Container\Container;

$container = new Container();

$container->set('logger', function () { return new Logger();});

//returns a new instance of Logger. Subsequent calls return the same object
$logger = $container->get('logger');

```

You also define multiple services by implementing `ServiceProviderInterface` and then implementing it's `getServices` method to return an associative array. If the element is a callable function it will be called and the result will be returned.

```
use Mooti\Container\ContainerAware;

class ServiceProvider implements ServiceProviderInterface
{
    public function getServices()
    {
        return [
            'logger'  => function () { return new Logger();},
            'message' => 'Hello World'},
        ];
    }
}

```

To us the container you will need to use the `ContainerAware` trait in you class. You will then be able to use the `get` method to get any items from the container

```
use Mooti\Container\ContainerAware;

class App
{
    use ContainerAware;

    public function run()
    {
    	$config  = $this->get('config');
    	$message = $this->get('message');

    	$logger->alert($message);
    }
}

```

So, putting it all together you will get something like:

```
use Mooti\Container\Container;
use Mooti\Container\ContainerAware;
use Mooti\Container\ServiceProvider\ServiceProviderInterface;

class App
{
    use ContainerAware;

    public function run()
    {
    	$config  = $this->get('config');
    	$message = $this->get('message');

    	$logger->alert($message);
    }
}

class ServiceProvider implements ServiceProviderInterface
{
    public function getServices()
    {
        return [
            'logger'  => function () { return new Logger();},
            'message' => 'Hello World'},
        ];
    }
}

$container       = new Container();
$serviceProvider = new ServiceProvider::class;
$app             = new App();

$container->registerServices($serviceProvider);
$app->setContainer($container);

// Should log "Hello World"
$app->run();

```

The ContainerAware trait extends functionality from [mooti/factory](https://github.com/mooti/factory). You can instantiate an object using the method `createNew` and it will try to inject the container into the object if it users the ContainerAware trait.

```
use Mooti\Container\Container;
use Mooti\Container\ContainerAware;
use Mooti\Container\ServiceProvider\ServiceProviderInterface;

class FooBar
{
    use ContainerAware;

    private $foo;
    private $bar;

    public function __construct($foo, $bar)
    {
        $this->foo = $foo;
        $this->bar = $bar;
    }

    public function logMessage()
    {
        $config  = $this->get('config');
        $message = $this->get('message');

        $logger->alert($this->foo . ' ' .  $this->bar . ' ' . $message);
    }
}

class App
{
    use ContainerAware;

    public function run()
    {
        $fooBar = $this->createNew(FooBar::class, ['test 1', 'test 2']);
        $fooBar->logMessage();
    }
}

class ServiceProvider implements ServiceProviderInterface
{
    public function getServices()
    {
        return [
            'logger'  => function () { return new Logger();},
            'message' => 'Hello World'},
        ];
    }
}

$container       = new Container();
$serviceProvider = new ServiceProvider::class;
$app             = new App();

$container->registerServices($serviceProvider);
$app->setContainer($container);

// Should log "test 1 test 2 Hello World"
$app->run();

```

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

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

Unknown

Total

1

Last Release

3635d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d03651fbb8a8c8e87446a872b0a21a4fb80589227bd3bf6802523f553992d53b?d=identicon)[lalobo](/maintainers/lalobo)

---

Top Contributors

[![lalobo](https://avatars.githubusercontent.com/u/736300?v=4)](https://github.com/lalobo "lalobo (2 commits)")

---

Tags

containerservicesservice container

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[bartacus/bartacus-bundle

Integrates the Symfony full-stack framework into TYPO3

1347.8k3](/packages/bartacus-bartacus-bundle)

PHPackages © 2026

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