[ Web Proxy ]
URL:
Viewing: https://docs.sonsofphp.com/components/container [Back]  [Original]

Container | Sons of PHP
[] []
[] []
On this page
For the complete documentation index, see llms.txt. This page is also available as Markdown.
Copy
On this page
  1. Components

Container

PSR-11 Container

Installation

Copy
composer require sonsofphp/container

Usage

Copy
<?php

use SonsOfPHP\Component\Container\Container;
use Psr\Container\ContainerInterface;

$container = new Container();
$container->set('service.id.one', function (ContainerInterface $container) {
    return new Service();
});
$container->set('service.id.two', function (ContainerInterface $container) {
    return new Service($container->get('service.id.one'));
});

// Services will not be created until they are called, once called, they will
// always return the same instance of the service. That means that in the
// following code, the "service.id.two" is only constructed once.
$service  = $container->get('service.id.two');
$service2 = $container->get('service.id.two');

Last updated 1 year ago


Web Proxy Viewer  |  New URL  |  Original Page