PHP code example of exts / canister

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

    

exts / canister example snippets


use Canister;

$container = new Canister;

$container['my-key-value'] = 'accessed anywhere';
 
echo $container->get('my-key-value'); // or $container['my-key-value'];

class Example
{
    public function foo() {
        return 'bar';
    }
}

$example = $container->get(Example::class);
echo $example->foo(); // -> 'bar'

$container->alias(ExampleInterface::class, Example::class);

$example = $container->get(ExampleInterface::class);
echo $example->foo(); // -> 'bar'

class Test implements TestInterface {
    public function example() {
        return 'example';
    }
}

class TestExample {
    public function __construct(TestInterface $test) {
        $this->test = $test;
    }
    public function testing() {
        return $this->test->example();
    }
}

$container->alias(TestInterface::class, Test::class);
$test_example = $container->get(TestExample::class);
echo $test_example->testing(); // -> 'example'

$container->factory(FactoryClass::class);

$container->factory(FactoryClass::class, function() {
    //...
});

$container->share('example', function($foo, $bar) {
    return $foo + $bar;
});

$container->define('example', [
    'foo' => val(5),
    'bar' => val(21)
]);

echo $container->get('example');

$container = new Canister;
$container->define(\PDO::class, [
    'dsn' => val('sqlite::memory:'),
]);

$pdo = $container->get(\PDO::class);

echo is_a($pdo, \PDO::class) ? 'true' : 'false'; // -> true