PHP code example of artex / di-container

1. Go to this page and download the library: Download artex/di-container library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

artex / di-container example snippets


use Artex\DIContainer\ServiceContainer;

$container = ServiceContainer::getInstance();

// Register a shared service
$container->singleton('logger', function () {
    return new Logger();
});

// Resolve the service
$logger = $container->get('logger');

$container->set('handler1', new Handler1(), true, ['event']);
$container->set('handler2', new Handler2(), true, ['event']);

// Retrieve all services tagged with "event"
$handlers = $container->getByTag('event');


$container->onRegister(function ($id, $concrete, $shared) {
    echo "Service {$id} registered.\n";
});

$container->onResolve(function ($id, $instance) {
    echo "Service {$id} resolved.\n";
});

$container->singleton('db', function () {
    return new DatabaseConnection();
});