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

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

technically/cascade-container
=============================

A simple yet powerful PSR-11 based service container implementation with layers and dependencies auto-wiring.

0.2.0(9mo ago)43MITPHPPHP ^8.0CI passing

Since Aug 4Pushed 9mo agoCompare

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

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

[![Project logo: image showing an onion with the text Technically Cascade Contianer](https://private-user-images.githubusercontent.com/370680/475960781-bc77d08e-8926-4734-9a59-7104775b1a99.jpg?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzU1Njc0MzEsIm5iZiI6MTc3NTU2NzEzMSwicGF0aCI6Ii8zNzA2ODAvNDc1OTYwNzgxLWJjNzdkMDhlLTg5MjYtNDczNC05YTU5LTcxMDQ3NzViMWE5OS5qcGc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwNDA3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDQwN1QxMzA1MzFaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lNzM0ZGM0M2Q0YmFhMTc5NDEwNDljZjc3OWZjNDY5NjI0MGRlM2Q0YzNhYjYwNjI5NzYxNDZlZTk0MjI5ZmUyJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.M6JFMQSKCI33REq6gE6U4HqpFakNa3y6cOYnefR5jRQ)](https://private-user-images.githubusercontent.com/370680/475960781-bc77d08e-8926-4734-9a59-7104775b1a99.jpg?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzU1Njc0MzEsIm5iZiI6MTc3NTU2NzEzMSwicGF0aCI6Ii8zNzA2ODAvNDc1OTYwNzgxLWJjNzdkMDhlLTg5MjYtNDczNC05YTU5LTcxMDQ3NzViMWE5OS5qcGc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwNDA3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDQwN1QxMzA1MzFaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lNzM0ZGM0M2Q0YmFhMTc5NDEwNDljZjc3OWZjNDY5NjI0MGRlM2Q0YzNhYjYwNjI5NzYxNDZlZTk0MjI5ZmUyJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.M6JFMQSKCI33REq6gE6U4HqpFakNa3y6cOYnefR5jRQ)Technically Cascade Container
=============================

[](#technically-cascade-container)

🧅 `Technically\CascadeContainer` is simple yet powerful PSR-11 based service container implementation with layers and dependencies auto-wiring.

[![Test](https://github.com/technically-php/cascade-container/actions/workflows/test.yml/badge.svg)](https://github.com/technically-php/cascade-container/actions/workflows/test.yml)

### Philosophy

[](#philosophy)

- [PSR Container](https://github.com/container-interop/fig-standards/blob/master/proposed/container.md) compatibility
- [Semantic Versioning](http://semver.org/)
- PHP 8.0+
- Minimal yet elegant API

### Features

[](#features)

- Inherits services from a parent PSR-11 Service Container
- [Can be forked into a new isolated container](#isolated-layers-forked-from-the-service-container), inheriting all services definitions from the original container
- [PSR Container](https://github.com/container-interop/fig-standards/blob/master/proposed/container.md) compatibility
- Autowiring — automatic dependencies resolution
- Full PHP 8.0+ features support for auto-wiring (e.g. union types)

```
use Technically\CascadeContainer\CascadeContainer;

$container = new CascadeContainer();

$container->set('config', $config);

// Lazy-evaluated services
$container->deferred('mailer', function () {
   // lazily initialize mailer service here
   $mailer =  /* ... */;

   return $mailer;
});

// On-demand object factories (executes every time 'request' is obtained from the container)
$container->factory('request', fn () => $requestFactory->createRequest());

// ✨ CASCADING LAYERS ✨

// Fork the container into an isolated layer, inheriting everything from above.
// Override services or define new ones. The changes won't affect the parent $container instance.
$environment = $container->cascade();

// For example, we want to use a different mailer implementation in the tests environment:
$environment->deferred('mailer', fn () => new NullMailer());
```

Usage
-----

[](#usage)

### Installation

[](#installation)

Use [composer](http://getcomposer.org/).

```
composer require technically/cascade-container
```

### Basics

[](#basics)

Checking presence, getting and setting service instances to the service container.

- `::get(string $id): mixed` — Get a service from the container by its name
- `::has(string $id): bool` — Check if there is a service defined in the container with the given name
- `::set(string $id, mixed $instance): void` — Define a service instance with the given name to the container

```
