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

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

luizfilipezs/container
======================

Dependency injection container

v1.0.5(3mo ago)04MITPHPPHP ^8.4

Since Mar 24Pushed 2mo ago1 watchersCompare

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

READMEChangelog (5)Dependencies (3)Versions (7)Used By (0)

luizfilipezs/container
======================

[](#luizfilipezscontainer)

This library implements a dependency injection system.

Minimum requirements
--------------------

[](#minimum-requirements)

- PHP 8.4
- Composer

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

[](#installation)

Inside your project folder, run:

```
composer require luizfilipezs/container
```

Usage
-----

[](#usage)

### Getting a definition

[](#getting-a-definition)

```
$container = new Container();
$container->set(MyInterface::class, MyClass::class);
$myObject = $container->get(MyInterface::class);
```

The above example creates a new instance of `MyClass`, typed as `MyInterface`.

### Setting a definition

[](#setting-a-definition)

#### Class string

[](#class-string)

```
$container->set(UserServiceInterface::class, UserService::class);
```

#### Class instance

[](#class-instance)

```
$container->set(UserServiceInterface::class, new UserService());
```

#### Closure

[](#closure)

```
$container->set(UserServiceInterface::class, fn() => new UserService());
```

### Checking for a definition

[](#checking-for-a-definition)

```
if ($container->has(UserServiceInterface::class)) {
```

### Removing a definition

[](#removing-a-definition)

```
$container->remove(UserServiceInterface::class);
```

### Setting a singleton

[](#setting-a-singleton)

Singleton classes are automatically recognized via `#[Singleton]` attribute.

```
use Luizfilipezs\Container\Attributes\Singleton;

#[Singleton]
class UserService
{
  public string $foo = 'bar';
}

$userService1 = $container->get(UserService::class);
$userService2 = $container->get(UserService::class);

$userService1->foo = 'baz';
echo $userService2->foo; // 'baz'
```

If you set a singleton class as definition for an interface, both the interface and the class will be set with the actual instance of the class as soon as it is created:

```
$container->set(UserServiceInterface::class, UserService::class);

$userServiceViaInterface = $container->get(UserServiceInterface::class);
$userServiceViaInterface->setFoo('baz');

$userServiceViaClass = $container->get(UserService::class);
$userServiceViaClass->getFoo(); // 'baz'
```

### Setting a class with lazy constructor

[](#setting-a-class-with-lazy-constructor)

Lazy constructor was natively implemented in PHP 8.4. It allows an object to be created without its `__construct` method getting called until an attribute is used.

```
use Luizfilipezs\Container\Attributes\Lazy;

#[Lazy]
class UserService
{
    public function __contruct(private readonly UserRepositoryInterface $userRepository)
    {
        echo 'Constructor was called.';
    }

    public function getAll(): array
    {
        return $this->userRepository->findAll();
    }
}

$userService = $container->get(UserService::class);
$users = $userService->getAll(); // 'Constructor was called.'
```

It is also possible to disable initialization on reading specific properties:

```
use Luizfilipezs\Container\Attributes\{Lazy, LazyInitializationSkipped};

#[Lazy]
class MyClass
{
    #[LazyInitializationSkipped]
    public string $skippedProp = 'foo';
    public string $normalProp = 'bar';

    public function __construct()
    {
        echo 'Constructor was called.';
    }
}

$myInstance = $container->get(MyClass::class);

$myInstance->skippedProp;
$myInstance->normalProp; // 'Constructor was called.'
```

#### Capturing lazy construction

[](#capturing-lazy-construction)

To know when a lazy `__construct` gets called you can call `Container::eventHandler`:

```
use Luizfilipezs\Container\Enums\ContainerEvent;

$container->eventHandler->on(
    event: ContainerEvent::LAZY_CLASS_CONSTRUCTED,
    callback: static function (string $className, object $instance) {
        echo "{$className}::__construct was called.";
    },
);

$instance = $container->get(MyClass::class);
$instance->foo; // 'MyClass::__construct was called.'
```

### Setting non-class definitions

[](#setting-non-class-definitions)

You can set a definition for any value, allowing it to get automatically injected even if it is not a class.

```
use Luizfilipezs\Container\Attributes\Inject;

enum ApiConstant: string
{
  case API_KEY = 'API_KEY';
  case API_SECRET = 'API_SECRET';
}

$container->setValue(ApiConstant::API_KEY, 'my_api_key');
$container->setValue(ApiConstant::API_SECRET, 'my_api_secret');

class ApiService
{
  public function __construct(
    #[Inject(ApiConstant::API_KEY)] string $apiKey,
    #[Inject(ApiConstant::API_SECRET)] string $apiSecret,
  ) {}
}

// call __construct with: 'my_api_key', 'my_api_secret'
$apiService = $container->get(ApiService::class);
```

Any type of value can be set, but it will be strictly compared to the parameter type:

```
$container->setValue('SOME_INT', 123);

class MyClass
{
  public function __contruct(#[Inject('SOME_INT')] float $value) {}
}

$object = $container->get(MyClass::class);
// ContainerException: Container cannot inject "SOME_INT". It is not the same
// type as the parameter. Expected float, got int.
```

#### Value definition methods

[](#value-definition-methods)

```
$container->setValue('key', 'value');
$value = $container->getValue('key'); // 'value'
$exists = $container->hasValue('key'); // true
$container->removeValue('key');
```

### Advanced options

[](#advanced-options)

`Container` constructor has three parameters:

- `strict` (defaults to `false`): if `true`, only definitions set explicitly (via `set()`) will be provided.
- `skipNullableClassParams` (defaults to `true`): if `true`, nullable constructor parameters typed as a class or an interface will always be set to `null`, except if the parameter has the `Inject` attribute bound to it.
- `skipNullableValueParams` (defaults to `true`): if `true`, nullable constructor parameters typed as a primitive type will always be set to `null`, except if the parameter has the `Inject` attribute bound to it.

#### Example with `strict`:

[](#example-with-strict)

```
$container = new Container(strict: true);

// ContainerException, because there is no explicit definition for "SomeClass"
$instance = $container->get(SomeClass::class);
```

#### Examples with `skipNullableClassParams`

[](#examples-with-skipnullableclassparams)

If `true`:

```
$container = new Container(skipNullableClassParams: true);
$instance = $container->get(SomeClass::class);

$instance->nullableDependency; // null
```

If `false`:

```
$container = new Container(skipNullableClassParams: false);
$instance = $container->get(SomeClass::class);

$instance->nullableDependency; // object
```

#### Examples with `skipNullableValueParams`

[](#examples-with-skipnullablevalueparams)

If `true`:

```
$container = new Container(skipNullableClassParams: true);
$instance = $container->get(SomeClass::class);

$instance->nullableString; // null
```

If `false`:

```
$container = new Container(skipNullableClassParams: false);
$instance = $container->get(SomeClass::class);

$instance->nullableString; // error, because string cannot be injected
```

In both examples above, **if the nullable parameter was bound to the `Inject` attribute, the value would be injected anyway**, because it forces injection.

Contributing
------------

[](#contributing)

### Forking

[](#forking)

At the top of the Github repository page, click the **Fork** button. Then clone the forked repository to your machine.

### Installing dependencies

[](#installing-dependencies)

```
composer install
```

### Testing

[](#testing)

Run:

```
./vendor/bin/phpunit tests
```

When making changes to the codebase, remember to create tests that cover all scenarios.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance82

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 65.9% 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 ~61 days

Recently: every ~75 days

Total

6

Last Release

114d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/03e7e845ccb4e389ce54d9734d66e114db3a01f496800e003310e85283c468b2?d=identicon)[luizfilipezs](/maintainers/luizfilipezs)

---

Top Contributors

[![luiz-itmapps](https://avatars.githubusercontent.com/u/199976087?v=4)](https://github.com/luiz-itmapps "luiz-itmapps (54 commits)")[![luizfilipezs](https://avatars.githubusercontent.com/u/17597514?v=4)](https://github.com/luizfilipezs "luizfilipezs (28 commits)")

---

Tags

composer-packagedependency-injectionphpphp8

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/luizfilipezs-container/health.svg)](https://phpackages.com/packages/luizfilipezs-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.

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

The Illuminate Container package.

31178.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)
