PHP code example of omerucel / di

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

    

omerucel / di example snippets





$environment = getenv('APPLICATION_ENV');

$di = new OU\DI();
$di->setShared('config', function ($di) use ($environment) {
    return new Config(realpath(__DIR__ . '/' . $environment . '.php');
});
$di->setShared('logger', function ($di) {
    return new Logger($di->get('config')->file_path);
});

/**
 * @var Logger $logger
 */
$logger = $di->get('logger');
$logger->info('Hello world!');
$di->reloadShared('logger')->info('Hello world!');



namespace Project\Service;

class ConfigService implements \OU\DI\Service
{
    public function getService(\OU\DI $di)
    {
        $environment = $di->get('environment');
        return new Config(realpath(__DIR__ . '/' . $environment . '.php');
    }
}


$di = new OU\DI();
$di->setShared('environment', 'development');
$di->setSharedService('config', 'Project\Service\ConfigService');
$config = $di->get('config');