PHP code example of rawebone / injector

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

    

rawebone / injector example snippets



use Psr\Log\LoggerInterface;

interface Injectable
{
    function __construct(LoggerInterface $logA, LoggerInterface $logB);
}



$container["logA"] = function ($container) { return new MyLogger($container["fileA"]); };
$container["logB"] = function ($container) { return new MyOtherLogger($container["fileB"]); };

$container["logB"]->warning("blah");



use Rawebone\Injector\Injector;

$injector = new Injector();

// By default, the injector will resolve to any callable
// with the name given as a broad brush approach:
function my_service()
{
    return new stdClass();
}

// Injection can be handled automatically by passing through
// a callable to the library

$injector->inject(function ($my_service)
{
    var_dump($my_service); // stdClass
});

// Injection can be handled manually by returning the service by name:

$my_service = $injector->service("my_service");

// Or simply getting arguments for the callable:

$args = $injector->argsFor(function ($my_service) {});
var_dump($args); // array("my_service" => stdClass);





$resolver = new Rawebone\Injector\RegisterResolver();
$resolver->register("serviceA", function () { return new MyService(); });
$resolver->register("serviceB", new \stdClass());

$resolver->registerMany(array(
    "serviceC" => new \stdClass(),
    "serviceD" => function () { return new MyService(); }
));

$injector->resolver($resolver);