PHP code example of studiow / autowire

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

    

studiow / autowire example snippets


class Configuration
{
    //...
}

interface HasConfiguration
{
    public function setConfiguration(Configuration $configuration);
    public function getConfiguration():Configuration
}

$container = new League\Container\Container();
$container->share(Configuration::class);

class AClassWithConfiguration implements HasConfiguration{
    //...
}

$container->share(AClassWithConfiguration::class, function() use($container){
    $obj = new AClassWithConfiguration();
    $obj->setConfiguration($container->get(Configuration::class));
    return $obj;
});

$container = new League\Container\Container();
$container->share(Configuration::class);

$awContainer = Studiow\Autowire\Container($container); 

$awContainer->attach(HasConfiguration::class, function($item, $awContainer){
   $item->setConfiguration($awContainer->get(Configuration::class)); 
});

class AClassWithConfiguration implements HasConfiguration{
    //...
}

$container->share(AClassWithConfiguration::class});

$awContainer = Studiow\Autowire\Container($container); 
$obj = $awContainer->get(AClassWithConfiguration::class);

$obj->getConfiguration();
//Configuration object was automatically injected

$obj = new AClassWithConfiguration();
$obj = $awContainer->applyCallbacks($obj);
$obj->getConfiguration();
//Configuration object was injected