PHP code example of constanze-standard / container

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

    

constanze-standard / container example snippets


 declare(strict_types=1);

use ConstanzeStandard\Container\Container;
use ConstanzeStandard\Container\Entity;

$container = new Container();
$entity = new Entity('foo', 'bar');
$container->addEntity($entity);

$foo = $container->get('foo');
var_dump($foo);  // foo

use ConstanzeStandard\Container\Container;
use ConstanzeStandard\Container\Entity;
use ConstanzeStandard\Container\Interfaces\EntityInterface;

$container = new Container();
$entity = new Entity('foo', function () {
    return 'bar';
}, EntityInterface::TYPE_DEFINITION);
$container->addEntity($entity);

$foo = $container->get('foo');
var_dump($foo);  // foo


$container->add('foo', function () {
    return 'bar';
}, EntityInterface::TYPE_DEFINITION);

$foo = $container->get('foo');
var_dump($foo);  // foo

$container->alias('aliasName', 'entityName');

$container->has('foo');  // 判断名为 foo 的 entity 是否存在
$container->remove('foo');  // 从 Container 中删除名为 foo 的 entity

$container->add('foo', function($foo) {
    return $foo;
}, EntityInterface::TYPE_DEFINITION);

$result = $container->make('foo', 'FOO VALUE');
var_dump($result);  // FOO VALUE

$container->add('foo', function ($container, $customArg) {
    return $customArg;
}, EntityInterface::TYPE_DEFINITION)->addArguments($container);

$container->make('foo', 'CustomArg');

$container->withEntityArguments($container);

$container->add('helloFoo', function ($container) {
    $foo = $container->get('Foo');
    return 'Hello ' . $foo;
}, EntityInterface::TYPE_DEFINITION)

use ConstanzeStandard\Container\AbstractEntityProvider;
use ConstanzeStandard\Container\Container;

class CustomEntityProvider extends AbstractEntityProvider
{
    protected $provides = [
        'foo'
    ];

    public function register(ContainerInterface $container)
    {
        $container->add('foo', function () {
            return 'bar';
        }, EntityInterface::TYPE_DEFINITION);
    }
}

$entityProvider = new CustomEntityProvider();
$container->addEntityProvider($entityProvider);

$container->get('foo');  // bar

...
use ConstanzeStandard\Container\Interfaces\BootableEntityProviderInterface;

class CustomEntityProvider extends AbstractEntityProvider implements BootableEntityProviderInterface
{
    protected $provides = [
        'foo'
    ];

    public function boot(ContainerInterface $container)
    {
        $container->add('publicService', function () {
            return new SomeService();
        }, EntityInterface::TYPE_DEFINITION);
    }

    public function register(ContainerInterface $container)
    {
        $container->add('foo', function () {
            return 'bar';
        }, EntityInterface::TYPE_DEFINITION);
    }
}

$entityProvider = new CustomEntityProvider();
$container->addEntityProvider($entityProvider);

$container = new Container;

$container['foo'] = 'bar';  // 添加一个名为 foo 的 entity
$foo = $container['foo'];  // 获取 foo 的内容
isset($container['foo']);  // 判断 foo 是否存在
unset($container['foo']);  // 从容器中删除 foo