1. Go to this page and download the library: Download uma/dic 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/ */
$container = new UMA\DIC\Container();
$container->set('dsn', '...');
// A database connection won't be made until/unless
// the 'db' service is fetched from the container
$container->set('db', static function(Psr\Container\ContainerInterface $c): \PDO {
return new \PDO($c->get('dsn'));
});
var_dump($container->resolved('db'));
// false
$pdo = $container->get('db');
var_dump($container->resolved('db'));
// true
$container = new UMA\DIC\Container();
// Normal lazy loaded service. Will always return the
// same object instance after running the Closure once.
$container->set('foo', static function(): \stdClass {
return new \stdClass();
});
// Factory service. The second argument must be a Closure.
// The closure will run every time the service is requested.
$container->factory('bar', static function(): \stdClass {
return new \stdClass();
});
// foo is always the same object instance.
var_dump($container->get('foo') === $container->get('foo'));
// true
// bar is a different object instance each time it's requested.
var_dump($container->get('bar') === $container->get('bar'));
// false
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.