PHP code example of pe / component-simple-di

1. Go to this page and download the library: Download pe/component-simple-di 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/ */

    

pe / component-simple-di example snippets



use PE\Component\SimpleDI\Container;
use PE\Component\SimpleDI\ServiceProviderInterface;

// Instantiate container
$di = new Container();

// Add simple value, can be any value
$di->set('foo', 'bar');
$di->get('foo'); //-> just return 'bar'

// Add service definition (closure factory)
$di->set('foo', $di->service(function () {
    return new \stdClass();
}));

$di->get('foo'); //-> call instantiator function and returns instance
$di->get('foo'); //-> returns instance same as in previous call

// Also you can register services via provider
// A. Create provider class
class SomeServiceProvider implements ServiceProviderInterface
{
    public function register(Container $container)
    {
        $container->set('foo', $container->service(function () {
            return new \stdClass();
        }));
    }    
}

// B. Register provider
$di->register(new SomeServiceProvider());

php composer.phar