PHP code example of nurse / di

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

    

nurse / di example snippets


$container = new Nurse\Container;

// Defining a dependency

$container->set('connection', function ($container) {
    $params = $container->get('connection_params');
    return new Connection($params);
})
->set('connection_params', function () {
    return array(
        'schema'   => 'someschema',
        'username' => 'root',
        'password' => 's3cr3t',
    );
});

// Retrieving the dependency (lazy loading)
$connection = $container->get('connection');

// alternatively you can use the singleton instance of the container

Nurse\Di::set('connection', function ($container) {
    $params = $container->get('connection_params');
    return new Connection($params);
})
->set('connection_params', function () {
    return array(
        'schema'   => 'someschema',
        'username' => 'root',
        'password' => 's3cr3t',
    );
});

$connection = Nurse\Di::get('connection');



namespace App;

use Nurse\Factory\FactoryInterface;
use Psr\Container\ContainerInterface;

class ConnectionFactory implements FactoryInterface
{
    public function createService(ContainerInterface $container)
    {
        $params = $container->get('connection_params');
        return new Connection($params);
    }

    public function getKey()
    {
        return 'connection';
    }
}

$factory = new \Dummy\MyDummyFactory();
$actual = $container->addFactory($factory);