PHP code example of kanian / containerx

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

    

kanian / containerx example snippets


class Car {
	protected $driver;
    public function __construct(Driver $driver)
    {
    	$this -> driver = $driver;
    }
    \\\ ... more car code
}

class HumanDriver implements Driver {
  public function drive()
  {
  	\\\ ... some driving code
  }
}

use Kanian\ContainerX\Container;

$container = new Container();
$container->set('chauffeur', function($c){ return new HumanDriver;});
$container->set('limo', function($c){ return new Car($c->get('chauffeur'));});

$limo = $container->get('limo');

use Kanian\ContainerX\Container;
$container = new Container();
$container->set('chauffeur',HumanDriver::class);
$container->set('limo',Car::class);
$limo = $container->get('limo');

use Kanian\ContainerX\ContainerX;

$container = new ContainerX();
$container['chauffeur'] =  HumanDriver::class;
$container['limo'] = Car::class;

$limo = $container['limo'];
 
singletonize
 
$container->singletonize('limo', Car::class);
$limo = $container['limo'];