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
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);