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

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

ghostwriter/container
=====================

Provides an extensible Dependency Injection Service Container for Automated Object Composition, Interception, and Lifetime Management.

6.0.1(6mo ago)15.8M—0.5%20BSD-4-ClausePHPPHP ~8.4.0 || ~8.5.0

Since Apr 25Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/ghostwriter/container)[ Packagist](https://packagist.org/packages/ghostwriter/container)[ Docs](https://github.com/ghostwriter/container)[ GitHub Sponsors](https://github.com/sponsors/ghostwriter)[ RSS](/packages/ghostwriter-container/feed)WikiDiscussions 6.0.x Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (48)Used By (20)

Container
=========

[](#container)

[![GitHub Sponsors](https://camo.githubusercontent.com/3a51d6febb644939a868f93d4af05ddec7e2f8761a721518032b5ca0096f3124/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73706f6e736f72732f67686f73747772697465723f6c6162656c3d53706f6e736f722b4067686f73747772697465722f636f6e7461696e6572266c6f676f3d4769744875622b53706f6e736f7273)](https://github.com/sponsors/ghostwriter)[![Automation](https://github.com/ghostwriter/container/actions/workflows/automation.yml/badge.svg)](https://github.com/ghostwriter/container/actions/workflows/automation.yml)[![Supported PHP Version](https://camo.githubusercontent.com/0a52286e41da67a534cb2ddac78fe40edb489aca2486c05153983d2db82beaa6/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f7068702f67686f73747772697465722f636f6e7461696e65723f636f6c6f723d383839326266)](https://www.php.net/supported-versions)[![Downloads](https://camo.githubusercontent.com/2672f9fc028d68e7f74ceff0204f71ae420b67b6e2923472eb47547fb1e14a7c/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f64742f67686f73747772697465722f636f6e7461696e65723f636f6c6f723d626c7565)](https://packagist.org/packages/ghostwriter/container)

Provides an extensible Dependency Injection Service Container for Automated Object Composition, Interception, and Lifetime Management.

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

[](#installation)

You can install the package via composer:

```
composer require ghostwriter/container
```

Usage
-----

[](#usage)

### Simple usage

[](#simple-usage)

Registering a service on the given container.

```
final readonly class Service
{
    public function __construct(
        private Dependency $dependency
    ) {}

    public function dependency():Dependency
    {
        return $this->dependency;
    }
}

$container = Container::getInstance();

$service = $container->get(Service::class);

assert($service instanceof Service); // true

assert($service->dependency() instanceof Dependency); // true
```

### Automatic Service Definition Registration

[](#automatic-service-definition-registration)

Important

A service definition class MUST implement `Ghostwriter\Container\Interface\Service\DefinitionInterface` [\[class\]](src/Interface/Service/DefinitionInterface.php).

Automatically register a service definition class using Composer's `extra` config in your `composer.json` file.

It should look like the following:

```
{
    "extra": {
        "ghostwriter": {
            "container": {
                "definition": "App\\Service\\Definition"
            }
        }
    }
}
```

### Service Definition

[](#service-definition)

Registering a service definition on the container.

```
interface TaskInterface {}
interface TaskCollectionInterface {
    public function add(TaskInterface $task): void;
    public function count(): int;
}

final readonly class MainTask implements TaskInterface {
    public function __construct(
        private string $name
    ) {}
}

final readonly class FirstTask implements TaskInterface {
    public function __construct(
        private string $name
    ) {}
}

final class TaskCollection implements TaskCollectionInterface
{
    private array $tasks = [];
    public function add(TaskInterface $task): void
    {
        $this->tasks[] = $task;
    }
    public function count(): int
    {
        return count($this->tasks);
    }
}
final class TaskCollectionFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container): TaskCollection
    {
        return new TaskCollection();
    }
}
final class TaskCollectionExtension implements ExtensionInterface
{
    /** @param TaskCollection $service */
    public function __invoke(ContainerInterface $container, object $service): void
    {
        $service->add(new FirstTask('Task 1'));

        $mainTask = $container->build(TaskInterface::class, ['name' => 'Main Task']);

        assert($mainTask instanceof MainTask); // true

        $service->add($mainTask);
    }
}

final readonly class TasksServiceDefinition implements DefinitionInterface
{
    public function __invoke(ContainerInterface $container)
    {
        $container->alias(MainTask::class, TaskInterface::class);
        $container->alias(TaskCollection::class, TaskCollectionInterface::class);
        $container->extend(TaskCollection::class, TaskCollectionExtension::class);
        $container->factory(TaskCollection::class, TaskCollectionFactory::class);
    }
}

$container = Container::getInstance();

$container->define(TasksDefinition::class);

$service = $container->get(TaskCollectionInterface::class);
assert($service instanceof TaskCollection); // true

assert($service->count() === 2); // true
```

### Contextual Bindings

[](#contextual-bindings)

Registering a Contextual Bindings on the container.

```
interface ClientInterface {}

final readonly class RestClient implements ClientInterface {}

final readonly class GraphQLClient implements ClientInterface {}

final readonly class GitHub
{
    public function __construct(
        private ClientInterface $client
    ) {
    }
    public function getClient(): ClientInterface
    {
        return $this->client;
    }
}

// When GitHub::class asks for ClientInterface::class, it would receive an instance of GraphQLClient::class.
$container->bind(GitHub::class, ClientInterface::class, GraphQLClient::class);

// When any other service asks for ClientInterface::class, it would receive an instance of RestClient::class.
$container->alias(ClientInterface::class, RestClient::class);
```

### Service Extensions

[](#service-extensions)

Registering a service extension on the container.

```
/**
 * @implements ExtensionInterface
 */
final readonly class GitHubExtension implements ExtensionInterface
{
    /**
     * @param GitHubClient $service
     */
    public function __invoke(ContainerInterface $container, object $service): void
    {
        $service->setEnterpriseUrl(
            $container->get(GitHubClient::GITHUB_HOST)
        );
    }
}

$container->alias(GitHubClientInterface::class, GitHubClient::class);
$container->extend(GitHubClientInterface::class, GitHubExtention::class);
```

### Service Factory

[](#service-factory)

Registering a service factory on the container.

```
final readonly class Dependency {}
final readonly class Service
{
    public function __construct(
        private Dependency $dependency
    ){}

    public function dependency():Dependency
    {
        return $this->dependency;
    }
}
final readonly class ServiceFactory {
  public function __invoke(Container $container): Service
  {
     return new Service($container->get(Dependency::class));
  }
}

$container = Container::getInstance();

$container->factory(Service::class, ServiceFactory::class);

$service = $container->get(Service::class);

assert($service instanceof Service); // true
assert($service->dependency() instanceof Dependency); // true
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG.md](./CHANGELOG.md) for more information what has changed recently.

### Security

[](#security)

If you discover any security related issues, please email `nathanael.esayeas@protonmail.com` instead of using the issue tracker.

Sponsors
--------

[](#sponsors)

\[[`Become a GitHub Sponsor`](https://github.com/sponsors/ghostwriter)\]

Credits
-------

[](#credits)

- [Nathanael Esayeas](https://github.com/ghostwriter)
- [All Contributors](https://github.com/ghostwriter/container/contributors)

License
-------

[](#license)

The BSD-3-Clause. Please see [License File](./LICENSE) for more information.

###  Health Score

60

—

FairBetter than 99% of packages

Maintenance81

Actively maintained with recent releases

Popularity45

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity79

Established project with proven stability

 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

Every ~34 days

Total

42

Last Release

54d ago

Major Versions

2.0.1 → 3.0.02024-02-06

3.0.3 → 4.0.02024-06-21

4.0.x-dev → 5.0.02025-02-21

5.0.x-dev → 6.0.02025-11-17

6.1.x-dev → 7.0.x-dev2026-03-25

PHP version history (11 changes)0.1.0PHP &gt;=8.0, &lt;8.2

0.5.0PHP &gt;=8.0,&lt;8.2

1.0.0PHP &gt;=8.0,&lt;8.3

1.3.0PHP &gt;=8.1,&lt;8.3

1.6.0PHP &gt;=8.1,&lt;8.4

2.0.0PHP &gt;=8.2

3.0.0PHP ^8.3

3.0.3PHP &gt;=8.3

4.0.x-devPHP &gt;=8.4

5.0.1PHP ^8.4

5.0.x-devPHP ~8.4.0 || ~8.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/1dbb0131801cc451dad9917ab29aa823b25d7eebc9f3875a9d481d109bdb44ee?d=identicon)[ghostwriter](/maintainers/ghostwriter)

---

Top Contributors

[![ghostwriter](https://avatars.githubusercontent.com/u/9754361?v=4)](https://github.com/ghostwriter "ghostwriter (1911 commits)")

---

Tags

containerdependency-injectionghostwriterphppsr-11service-containercontainerghostwriter

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[pimple/pimple

Pimple, a simple Dependency Injection Container

2.7k130.5M1.4k](/packages/pimple-pimple)[league/container

A fast and intuitive dependency injection container.

86787.8M343](/packages/league-container)[php-di/php-di

The dependency injection container for humans

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

A serializable dependency injection container with constructor and setter injection, interface and trait awareness, configuration inheritance, and much more.

356968.3k58](/packages/aura-di)[league/tactician-container

Tactician integration for any container implementing PSR-11

7710.1M23](/packages/league-tactician-container)[acclimate/container

Provides adapters for various third-party service containers.

219390.6k15](/packages/acclimate-container)

PHPackages © 2026

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