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

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

marcelomx/micro-container
=========================

A micro Dependency Injection Container

1.0.0-beta(2y ago)02MITPHP

Since May 15Pushed 1y ago1 watchersCompare

[ Source](https://github.com/marcelomx/micro-container)[ Packagist](https://packagist.org/packages/marcelomx/micro-container)[ RSS](/packages/marcelomx-micro-container/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

Micro Container
===============

[](#micro-container)

Micro Container is a lightweight PHP Dependency Injection Container designed to simplify the management of dependencies in your PHP applications. With Micro Container, you can easily define and resolve dependencies using a clean and intuitive syntax.

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

[](#installation)

To install Micro Container, use [Composer](https://getcomposer.org/):

```
composer require marcelomx/micro-container
```

Usage
-----

[](#usage)

### Basic Setup

[](#basic-setup)

Start by creating an instance of the `ServiceContainer` class and provide it with an array containing dependency definitions:

```
use MicroContainer\ServiceContainer;

$container = new ServiceContainer([
    Foo::class => fn() => new Foo('foo', new \stdClass),
    Bar::class => Bar::class,
    'foo.alias' => Foo::class,
    FooInterface::class => Foo::class
]);
```

Here's a breakdown of the definitions:

- **Callable Factory (`Foo::class`):** Using a callable factory function to define the instantiation of the `Foo` class, allowing for custom initialization.
- **Class String Autowiring (`Bar::class`):** Demonstrating class string autowiring resolution for the `Bar` class, where Micro Container automatically resolves dependencies.
- **Service Alias (`'foo.alias'`):** Creating an alias for the `Foo` class to demonstrate the usage of service aliases in the container.
- **Interface Resolution (`FooInterface::class`):** Resolving an interface (`FooInterface`) to its implementation (`Foo`).

### Retrieving Services

[](#retrieving-services)

Once the container is set up, you can easily retrieve instances of your defined services:

```
$foo = $container->get(Foo::class);
assert($foo instanceof Foo);

$aliasFoo = $container->get('foo.alias');
assert($aliasFoo === $foo);

$bar = $container->get(Bar::class);
assert($bar instanceof Bar);
assert($bar->foo === $foo);

$service = $container->get(FooInterface::class);
assert($service instanceof FooInterface);
assert($service === $foo);
```

### Autowiring

[](#autowiring)

Micro Container supports autowiring for non-defined services. In the example below, the `Baz` class is created without explicitly defining it in the container:

```
$baz = $container->get(Baz::class);
assert($baz instanceof Baz);
assert($baz->bar === $bar);
```

This showcases Micro Container's ability to automatically resolve dependencies, making it convenient to work with classes that are not explicitly defined in the container.

Additionally, you can use the Autowired attribute to automatically inject dependencies into your class properties:

```
use MicroContainer\Attributes\Autowired;

class MyClass {
    #[Autowired()]
    private FooInterface $foo;

    #[Autowired(service: Bar::class)]
    private $bar;
}
```

In this example, the Autowired attribute is utilized to indicate that the $foo property should be automatically injected with an instance of FooInterface, and the $bar property should be injected with an instance of the Bar class. This provides a concise and declarative way to express dependencies within your class, improving readability and reducing boilerplate code.

Feel free to customize and expand on this basic setup to meet the specific requirements of your project.

Copyright
---------

[](#copyright)

Micro Container is open-source software released under the [MIT License](LICENSE).

Contribution
------------

[](#contribution)

We welcome contributions! To contribute to Micro Container, please follow our [contribution guidelines](CONTRIBUTING.md). Your help is highly appreciated.

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

 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

732d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/78e6a4a8084953912530d5b209dcc1795a6ced39afa49cd911ed8c27d0497ec6?d=identicon)[marcelomx](/maintainers/marcelomx)

---

Top Contributors

[![marcelomx](https://avatars.githubusercontent.com/u/254518?v=4)](https://github.com/marcelomx "marcelomx (14 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k431.1M7.5k](/packages/symfony-dependency-injection)[illuminate/contracts

The Illuminate Contracts package.

704122.9M10.1k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31278.1M2.0k](/packages/illuminate-container)[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)

PHPackages © 2026

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