PHPackages                             haszi/dicontainer - 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. haszi/dicontainer

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

haszi/dicontainer
=================

A learning exercise of a PSR-11 compatible dependency injection container

1.0.0(2y ago)15PHPPHP &gt;=8.0

Since Oct 28Pushed 2y ago1 watchersCompare

[ Source](https://github.com/haszi/dicontainer)[ Packagist](https://packagist.org/packages/haszi/dicontainer)[ RSS](/packages/haszi-dicontainer/feed)WikiDiscussions master Synced today

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

DI Container
============

[](#di-container)

A basic PSR-11 compatible dependency injection container written to learn about the concepts and implementation of object composition, lifetime managmenent and interception provided by DICs. Basic object composition and lifetime management functionality has been implemented (see [features](#supported) and [limitations](#not-supported)) with no support for interception of calls to managed objects.

### Requirements

[](#requirements)

PHP 8.0+

### Installation

[](#installation)

Install with Composer

```
composer require haszi/dicontainer

```

Getting started
---------------

[](#getting-started)

### Create a container

[](#create-a-container)

```
use haszi\DI\Container;

$container = new Container();
```

### PSR-11 compatibility

[](#psr-11-compatibility)

```
if ($container->has('MyClass')) {
  $myObject = $container->get('MyClass');
}
```

### Register transient services

[](#register-transient-services)

Transient services (returning a new instance each time you get them from the container) can be registered using a string representing a classname or a closure.

Classname

```
$container->set('comment', Comment::class);
```

Closure

```
$container->set(Comment::class, function () {
  return new Comment();
});
```

### Register shared services

[](#register-shared-services)

Shared services (returning the same instance each time you get it from the container)can be registered using a string representing a classname, a closure or an instantiated object. All shared service objects are lazily initialized on first use.

Classname

```
$container->setShared('database', DatabaseConnection::class);
```

Closure

```
$container->setShared(DatabaseConnection::class, function () {
  return new DatabaseConnection();
});
```

Instantiated Object

```
$db = new DatabaseConnection();

$container->setShared(DatabaseConnection::class, $db);
```

### Register scoped services

[](#register-scoped-services)

Scoped services are identical to shared service with the exception that the instantiated objects can be flushed without deregistering the service itself. A common use for scoped services are request objects in an event loop.

Classname

```
$container->setScoped('request', Request::class);
```

Closure

```
$container->setScoped(Request::class, function () {
  return new Request();
});
```

Instantiated Object

```
$request = new Request();

$container->setScoped(Request::class, $request);
```

### Flush initialized scoped objects

[](#flush-initialized-scoped-objects)

```
$container->unsetScopedInstances(Request::class);
```

### Deregister services

[](#deregister-services)

```
$container->unset(DatabaseConnection::class);
```

Features
--------

[](#features)

### Supported

[](#supported)

- object composition
    - constructor injection
        - auto-wiring
- lifetime management
    - transient services (new object returned on each get request)
    - shared services (the same object returned on each get request)
    - scoped services (the same object returned on each get request with the instantiated object being disposable)

### Not supported

[](#not-supported)

- object composition
    - setter and property injection
    - injection of:
        - union and intersection types
        - passing in values to built-in types (array, callable, bool, float, int, string, iterable, object, mixed)
- interception

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

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

978d ago

### Community

Maintainers

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

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/haszi-dicontainer/health.svg)

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

###  Alternatives

[symfony/symfony

The Symfony PHP framework

31.4k87.2M2.2k](/packages/symfony-symfony)[symfony/dependency-injection

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

4.2k455.6M9.5k](/packages/symfony-dependency-injection)[symfony/contracts

A set of abstractions extracted out of the Symfony components

3.9k65.9M137](/packages/symfony-contracts)[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k51.2M335](/packages/api-platform-core)[moonshine/moonshine

Laravel administration panel

1.3k253.1k79](/packages/moonshine-moonshine)[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k49](/packages/ecotone-ecotone)

PHPackages © 2026

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