technically/null-container

Null (always empty) PSR-11 container implementation

2.0.0 2021-03-31 11:06 UTC

This package is auto-updated.

Last update: 2024-10-14 20:36:23 UTC


README

Technically\NullContainer is a PSR-11 container implementation that is always empty.

This is a NullObject pattern implementation for PSR-11.

This is useful when you want to provide an optional ContainerInterface dependency, but don't want to deal with nullable values.

Tests Status

Features

  • PSR-11
  • PHP 8.0+
  • Semver
  • Tests
  • Changelog

Installation

Use Composer package manager to add NullContainer to your project:

composer require technically/null-container

Example

use Psr\Container\ContainerInterface;
use Technically\NullContainer\NullContainer;

final class MyServiceContainer implements ContainerInterface
{
    private ContainerInterface $parent;

    /**
     * @param ContainerInterface|null $parent
     */
    public function __construct(ContainerInterface $parent = null)
    {
        $this->parent = $parent ?? new NullContainer();
    }

    // ... your code, where you don't need to deal with $parent set to `null`.
}

Changelog

All notable changes to this project will be documented in the CHANGELOG file.

Credits