PHP code example of phower / container

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

    

phower / container example snippets


// in a bootstrap create the container and set a dependency
use Phower\Container\Container;

$container = new Container();
$container->set('some', new SomeClass());
$container->set('another', new AnotherClass());

// later inside our class aware of the container
if ($container->has('some') {
    /* @var $some SomeClass */
    $some = $container->get('some');
}

// in a bootstrap create the container and set a dependency
use Phower\Container\Container;

$container = new Container();
$container->add('some', 'SomeClass');
$container->add('another', 'AnotherClass');

// later inside our class aware of the container
if ($container->has('some') {
    /* @var $some SomeClass */
    $some = $container->get('some');
}

use Phower\Container\FactoryInterface;
use Phower\Container\ContainerInterface;

class SomeFactory implments FactoryInterface
{
    public function create(ContainerInterface $container)
    {
        $arg1 = 123;
        $arg2 = $container->get('arg2');
        return new SomeClass($arg1, $arg2);
    }
}

// in a bootstrap create the container and set a dependency
use Phower\Container\Container;

$container = new Container();
$container->add('arg2', 'AnotherClass');
$container->addFactory('some', 'SomeFactory');

// later inside our class aware of the container
if ($container->has('some') {
    /* @var $some SomeClass */
    $some = $container->get('some');
}

use Phower\Container\AbstractFactoryInterface;
use Phower\Container\ContainerInterface;

class SomeAbstractFactory implments AbstractFactoryInterface
{
    public function canCreate(ContainerInterface $container, $name)
    {
        return substr($name, 0, 5) === 'Some\\';
    }

    public function create(ContainerInterface $container, $name)
    {
        return new $name();
    }
}

// in a bootstrap create the container and set a dependency
use Phower\Container\Container;

$container = new Container();
$container->addAbstractFactory('some', 'SomeAbstractFactory');

// later inside our class aware of the container

/* @var $one Some\OneClass */
$one = $container->get('Some\OneClass');

/* @var $other Some\OtherClass */
$other = $container->get('Some\OtherClass');

/* @var $third Some\ThirdClass */
$third = $container->get('Some\ThirdClass ');

// in a bootstrap create the container and set a dependency
use Phower\Container\Container;

$container = new Container();
$container->add('some', 'SomeClass');
$container->addAlias('something', 'some');

// later inside our class aware of the container
if ($container->has('something') {
    /* @var $something SomeClass */
    $something = $container->get('something');
}

// in a bootstrap create the container and set a dependency
use Phower\Container\Container;

$container = Container::create([
    'classes' => [
        'some' => 'SomeClass',
        // more classes ...
    ],
    'factories' => [
        'some-factory' => 'SomeFactory',
        // more factories ...
    ],
    'abstract_factories' => [
        'abstract-factory' => 'SomeAbstractFactory',
        // more abstract factories ...
    ],
    'aliases' => [
        'alis-to-some' => 'some',
        // more aliases ...
    ],
]);