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

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

maplephp/container
==================

Container, Factories and dependency injectors with full PSR-11 support.

v2.1.0(1mo ago)25086Apache-2.0PHPPHP &gt;=8.2

Since Nov 29Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/MaplePHP/Container)[ Packagist](https://packagist.org/packages/maplephp/container)[ Docs](https://wazabii.se)[ RSS](/packages/maplephp-container/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (4)Versions (10)Used By (6)

Container, Factories and the dependency injector
================================================

[](#container-factories-and-the-dependency-injector)

PSR Container built for MaplePHP framework

Container, Factories and dependency injectors will help to make your PHP code more maintainable, flexible, and testable by reducing coupling between objects and centralizing the management of dependencies.

- [Container](#container)
- [Factory](#factory)
- [Dependency injector](#dependency-injector)
- [Event handler](#event-handler)

Container
---------

[](#container)

Containers allowing you to easily create and retrieve objects that are needed throughout your application.

```
use MaplePHP\Container\Container;

$container = new Container();

// You can set mixed values in the container
$container->set("hasEmail", true);

$hasEmail = $container->get("hasEmail")->get();
var_dump($hasEmail); // Result in: (bool) true
```

If the constructor of "YourClass" contains unresolved class arguments, the dependency injector will attempt to automatically locate them for you. Read more under the headline **dependency injector**.

Factory
-------

[](#factory)

Factories can be used to create new instances of objects, rather than instantiating them directly in your code.

```
$container->factory("factoryKey", function() {
    $a = new TestClassA();
    $b = new TestClassB();
    return new TestClassC($a, $b);
});
echo $container->get("factoryKey"); // Will return TestClassC
```

Dependency injector
-------------------

[](#dependency-injector)

Dependency injection is a technique for managing dependencies between objects in an application. Instead of creating objects directly in your code, you can pass them in as dependencies when you instantiate an object. This makes your code more modular and easier to test, as you can easily swap out dependencies for mock objects or other implementations.

You can use the **Dependency injector** just like create any other container, as long as you dont add arguments or try to access method, if you do that then it will automatically disable **Dependency injector**. It is design like this becouse it will load in all class reclusive into endlessly.

Take a look at this example

```
use MaplePHP\Container\Container;
use MaplePHP\Container\Autowire;
use App\Services\MailService;

$container = new Container();
$container->set("MailService", new Autowire(MailService::class));

// This will return "MailService" with all dependencies resolved on constructor
$mailService = $container->get("MailService")->get();
echo $mailService->send();
```

Event handler
-------------

[](#event-handler)

### Initiate

[](#initiate)

```
use MaplePHP\Container\EventHandler;
```

### Example 1 - (Callable)

[](#example-1---callable)

```
$logger = new EventHandler();
$logger->addHandler(new Logger());
$logger->addEvent(function() {
    var_dump("Executed in conjunction with logger every method");
    // You could add a mail function that will send log message to you,
});
echo $logger->error("A error message to log");
// Will log message AND execute the event
```

### Example 2 - (Bind to method)

[](#example-2---bind-to-method)

```
$logger = new EventHandler();
$logger->addHandler(new Logger(), ["emergency", "alert", "critical"]);
$logger->addEvent(function() {
    var_dump("Executed in conjunction with logger event method");
    // You could add a mail function that will send log message to you,
});
echo $logger->error("A error message to log");
// Will only log message
echo $logger->alert("A error message to log");
// Will log message AND execute the event
```

### Example 3 - (Add service to handler)

[](#example-3---add-service-to-handler)

Set the namespace to the **EventInterface**.

```
use MaplePHP\Container\Interfaces\EventInterface;
```

Then extend a class with implements to the interface **EventInterface** and add the method **resolve** to that class. I am using a "Anonymous Function" bellow as an example just to show that **EventInterface** is required, you can use a regular class.

```
$callableFunction = new class implements EventInterface {

    public function someMethod(string $what): string
    {
        return "Hello {$what}!";
    }

    // Resolve method will be executed in conjunction
    // with logger event method
    public function resolve(): void
    {
        var_dump($this->someMethod("world"));
    }
};

$logger = new EventHandler();
$logger->addHandler(new Logger(), ["emergency", "alert", "critical"]);
$logger->addEvent(function() {
    var_dump("Executed in conjunction with logger event method");
    // You could add a mail function that will send log message to you,
});
echo $logger->error("A error message to log");
// Will only log message
echo $logger->alert("A error message to log");
// Will log message AND execute the event
```

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance90

Actively maintained with recent releases

Popularity19

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 69.6% 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 ~121 days

Recently: every ~208 days

Total

8

Last Release

51d ago

Major Versions

v1.1.4 → v2.0.02026-01-12

PHP version history (2 changes)v1.0.0PHP &gt;=8.0

v2.0.0PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/724b188e785081275926c5b9c07082e2b3f4afb797efdda61eb1630457e17824?d=identicon)[wazabii](/maintainers/wazabii)

---

Top Contributors

[![wazabii8](https://avatars.githubusercontent.com/u/6400238?v=4)](https://github.com/wazabii8 "wazabii8 (16 commits)")[![CreativeWaDev](https://avatars.githubusercontent.com/u/153771800?v=4)](https://github.com/CreativeWaDev "CreativeWaDev (4 commits)")[![danielRConsid](https://avatars.githubusercontent.com/u/169045496?v=4)](https://github.com/danielRConsid "danielRConsid (3 commits)")

---

Tags

containerPSR-11factoriesdependency injectorsinjectors

### Embed Badge

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

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

###  Alternatives

[php-di/php-di

The dependency injection container for humans

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

A flexible dependency injection container

20260.4k6](/packages/slince-di)[chubbyphp/chubbyphp-container

A simple PSR-11 container implementation.

1978.4k14](/packages/chubbyphp-chubbyphp-container)[phpwatch/simple-container

A fast and minimal PSR-11 compatible Dependency Injection Container with array-syntax and without auto-wiring

1810.1k2](/packages/phpwatch-simple-container)[selective/container

A simple PSR-11 container implementation with autowiring.

1220.4k4](/packages/selective-container)[devanych/di-container

Simple implementation of a PSR-11 dependency injection container

124.2k3](/packages/devanych-di-container)

PHPackages © 2026

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