PHP code example of tobento / service-autowire

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

    

tobento / service-autowire example snippets


use Tobento\Service\Autowire\Autowire;

// Autowiring an object
$foo = (new Autowire($container))->resolve(Foo::class);

// Call method using autowiring
$value = (new Autowire($container))->call([Foo::class, 'method']);

use Tobento\Service\Autowire\Autowire;

// By name
$foo = (new Autowire($container))->resolve(Foo::class, ['name' => 'value']);

// By position
$foo = (new Autowire($container))->resolve(Foo::class, [2 => 'value']);

use Tobento\Service\Autowire\Autowire;
use Tobento\Service\Autowire\AutowireException;

try {
    $foo = (new Autowire($container))->resolve([Foo::class, 'method']);
} catch (AutowireException $e) {
    // not resolvable
}

use Tobento\Service\Autowire\Autowire;

// Using array callable
$value = (new Autowire($container))->call([Foo::class, 'method'], ['name' => 'value']);

// Using closure
$value = (new Autowire($container))->call(function(Foo $foo, $name) {
    return $name;
}, ['name' => 'value']);

var_dump($value); // string(5) "value"

// Using class with __invoke
$value = (new Autowire($container))->call(Invokable::class, ['name' => 'value']);

// Using Class::method syntax
$value = (new Autowire($container))->call('Foo::method', ['name' => 'value']);