PHP code example of gdianov / opium

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

    

gdianov / opium example snippets


//$t is instance of: gdianov\opium\tests\classes\T  
$t = $opium->make('t'); 

//$c is instance of: gdianov\opium\tests\classes\C with 
//injected object $t by constructor
$c = $opium->make('c');  

//$p is instance of: gdianov\opium\tests\classes\P with
//injected object $c by property and string by constructor
$p = $opium->make('p'); 

//You can injected dependency by property and constructor.


$t = $opium->makeDynamic([
     'class' => T::class,
     'props' => [
          [
            'bar' => $barValue
          ]
]);
                
 

$c = $opium->makeDynamic([         
    'class' => C::class,
    'constructor' => ['@t']   
]);       

//New C instance with T dependency      

$t = $opium->getWithParams('t', [
        'props' => [
            ['bar' => 'Another Value'],
        ]
]);

//Instance T with new property bar value
             

    $configFile = __DIR__ . '/config.yaml';
    $loader = new YamlLoader($configFile);
    $config = $loader->configure();
    $opium = Opium::getInstance(new Container(), $config);