PHP code example of fas / autowire

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

    

fas / autowire example snippets




use Fas/Autowire/Autowire;

$autowire = new Autowire();

// Autowire all constructor arguments
$myObject = $autowire->new(MyClass::class);

// Override some constructor arguments
$myObject = $autowire->new(MyClass::class, ['some_argument' => 'test-value']);

// Call a method with no arguments (autowire all arguments)
$autowire->call(function (DateTime $datetime, MyClass $myObject) {
    // do stuff
});

// Override argument
$autowire->call(function (DateTime $datetime, MyClass $myObject) {
    // do stuff
}, ['datetime' => new DateTime('2021-07-01 12:34:56')]);

// Any callable will do
// Plain function
$upperCased  = $autowire->call('strtoupper', ['str' => 'something-in-lower-case']);

// Static method
$datetime = $autowire->call([DateTime::class, 'createFromFormat'], [
    'format' => 'Y-m-d H:i:s',
    'time' => '2021-01-02 12:34:56', // php7
    'datetime' => '2021-01-02 12:34:56', // php8
    'object' => new DateTimeZone('Europe/Copenhagen'),
]);

// Instance method
$autowire->call([new DateTime, 'setTime'], ['hour' => 0, 'minute' => 1, 'second' => 2, 'microseconds' => 123]);

// Invokable class
$autowire->call($myInvokableClass, ['my_param' => 'test']);



$container = new MyPsrContainer();
$autowire = new Autowire($container);



$container = new Container();
$container->set(LoggerInterface::class, NullLogger::class);

$autowire = new Autowire($container);
$autowire->call(function (LoggerInterface $logger) {
    $logger->info("Logging using whatever logger is defined for LoggerInterface in the container");
});