PHP code example of kocuj / di

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

    

kocuj / di example snippets


use Kocuj\Di\Di;
use Kocuj\Di\Service\ServiceType\ServiceType;

$di = new Di();

$defaultContainer = $di->getDefault();

$myContainer = $di->create();

$myContainer = $di->copy($oldContainer);

$myContainer->addShared('someService', \Services\Service::class);

$myContainer->addShared('otherService', \Services\OtherService::class, [
    [
        'type' => 'service',
        'value' => \Services\Service::class
    ],
    [
        'type' => 'value',
        'value' => true
    ]
]);

$myContainer->get('otherService');

$myContainer->getOtherService();



use Kocuj\Di\Di;

// initialize DI container
$di = new Di();
// get DI container
$container = $di->getDefault();
// set DI services
$container->addShared('input', InputService::class);
$container->addShared('output', OutputService::class);
$container->addStandard('main', Main::class, [
    [
        'type' => 'service',
        'value' => 'input'
    ],
    [
        'type' => 'service',
        'value' => 'output'
    ]
]);
// execute
$container->get('main')->display();