PHP code example of kenphp / container

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

    

kenphp / container example snippets



use Ken\Container\Container;

$container = new Container();

// Sets item with identifier `Mock1`
$container->set(Mock1::class, function(Container $c) {
    return new Mock1();
});

// Sets item with identifier `Mock2`
$container->set(Mock2::class, function(Container $c) {
    $m1 = $c->get(Mock1::class);
    return new Mock2($m1);
});

// Sets item with identifier `app_name`
$container->set('app_name', 'Application Title');

// Gets item with identifier `Mock1`
$mock1 = $container->get(Mock1::class);

$mock2 = $container->get(Mock2::class);

echo $container->get('app_name'); // prints `Application Title`