PHP code example of ellipse / container-reflection

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

    

ellipse / container-reflection example snippets




namespace App;

interface SomeInterface
{
    //
}



namespace App;

class SomeClass implements SomeInterface
{
    //
}



namespace App;

class SomeOtherClass
{
    public function __construct(SomeInterface $class1, YetSomeOtherClass $class2)
    {
        //
    }
}



namespace App;

class YetSomeOtherClass
{
    //
}



use Some\Psr11Container;

use Ellipse\Container\ReflectionContainer;

use App\SomeInterface;
use App\SomeClass;
use App\SomeOtherClass;
use App\YetSomeOtherClass;

// Get an instance of some Psr-11 container.
$container = new Psr11Container;

// Add some definitions into the original container.
$container->set('some.value', function () {

    return 'something';

});

$container->set(SomeInterface::class, function () {

    return new SomeClass;

});

// Decorate the container.
$container = new ReflectionContainer($container);

// Now ->has() returns true for all those aliases:
$container->has('some.value');
$container->has(SomeInterface::class);
$container->has(SomeClass::class);
$container->has(SomeOtherClass::class);
$container->has(YetSomeOtherClass::class);

// ->get() still returns the values contained in the original container:
$container->get('some.value'); // returns 'something'
$container->get(SomeInterface::class); // returns the defined instance of SomeClass

// now ->get() can also build instances of non contained classes.
// Here an instance of SomeOtherClass is build by injecting the contained implementation of SomeInterface and a new instance of YetSomeOtherClass.
$container->get(SomeOtherClass::class);

// On multiple call the same instance is returned.
$someotherclass1 = $container->get(SomeOtherClass::class);
$someotherclass2 = $container->get(SomeOtherClass::class);

$someotherclass1 === $someotherclass2; // true



namespace App\Controllers;

interface ControllerInterface
{
    //
}



namespace App\Controllers;

use App\SomeDependency;

class SomeController implements ControllerInterface
{
    private $dependency;

    public function __construct(SomeDependency $dependency)
    {
        $this->dependency = $dependency;
    }

    public function index()
    {
        //
    }
}



namespace App\Controllers;

use App\SomeOtherDependency;

class SomeOtherController implements ControllerInterface
{
    private $dependency;

    public function __construct(SomeOtherDependency $dependency)
    {
        $this->dependency = $dependency;
    }

    public function index()
    {
        //
    }
}



use Some\Psr11Container;

use Ellipse\Container\ReflectionContainer;

use App\Controllers\ControllerInterface;
use App\Controllers\SomeController;
use App\Controllers\SomeOtherController;
use App\SomeDependency;
use App\SomeOtherDependency;

// Get an instance of some Psr-11 container.
$container = new Psr11Container;

// Register 'App\SomeDependency' into the original container.
$container->set(SomeDependency::class, function () {

    return new SomeDependency;

});

// Allow auto wiring only for classes implementing ContainerInterface.
$container = new ReflectionContainer($container, [ControllerInterface::class]);

// This returns an new instance of SomeController with the defined instance of SomeDependency injected.
$controller = $container->get(SomeController::class);

// This fails because as SomeOtherDependency is not defined in the original container.
$controller = $container->get(SomeOtherController::class);