PHP code example of xynha / dicontainer

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

    

xynha / dicontainer example snippets


// Without configuration
$dic = new DiContainer(new DiRuleList);

$obj = $this->dic->get('classname');

// Construct rule list
$rlist = new DiRuleList();

// Add configuration from file
$json = json_decode(file_get_contents('filename'), true);
$rlist = $rlist->addRules($json);

// Add configuration manually
$rule['shared'] = true;
$rlist = $rlist->addRule('classname', $rule);

// Construct the container
$dic = new DiContainer($rlist);

$obj = $this->dic->get('classname');

    $rlist = new DiRuleList();
    $rule['interfaceA'] = ['instanceOf' => $object];
    $rlist = $rlist->addRules($rule);
    

  class Config{
    public function getConfig(string $key){
      if ($key === 'db'){
        return 'dbconfig';
      }
    }
  }

  class DatabaseDriver{
    public function __construct(string $config){}
  }
  

  $config = new Config();
  $driver = new DatabaseDriver($config->getConfig('db'));
  

  $driver = $dic->get(DatabaseDriver::class);