PHP code example of dc / ioc

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

    

dc / ioc example snippets


$container = new \DC\IoC\Container();
$container
	->register('\Car') // fully qualified namespace
    ->to('\IVehicle')
    ->withContainerLifetime(); // lives as long as the container

$container
	->register(new \Car())
	->to('\IVehicle');

$container
	->register(function() {
		new \Car();
	})->to('\IVehicle');

$vehicle = $container->resolve('\IVehicle');

$vehicles = $container->resolveAll('\IVehicle');

class Trailer {
	public __construct(\IVehicle $vehicle) {
		// attach your Trailer to you IVehicle, or something
	}
}

$container->resolve("Trailer"); // you don't even have to register Trailer

class Trailer {
    /**
     * @param $matchesVehicles array|\IVehicle[]
     */
	public __construct(array $matchesVehicles) {

	}
}

class Trailer {
    /**
     * @param $matchesVehicles \IVehicle[]
     */
	public __construct($matchesVehicles) {

	}
}

class Car {
   /**
    * @inject
    * @var \Trailer
    */
   private $trailer;
}

$car = new Car();
$container->inject($car);

$container->register(function(\IFoo $foo) {
    return new Bar($foo);
})->to('\Bar');

$container->register(
	/**
     * @param $foos \IFoo[]
     */
	function(array $foos) {
		return new Bar($foos);
	})->to('\Bar');

class MyModule extends \DC\IoC\Modules\Module {
    public __construct() {
        parent::__construct("package-name", ["dc/router"]);
    }
    
    public register(\DC\IoC\Container $container) {
        $container->register('\Foo')->to('\Bar');
    }
}

$container->registerModules([new MyModule(), new \DC\Router\Module()]);