PHP code example of krak / invoke

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

    

krak / invoke example snippets




interface Invoke {
    public function invoke($func, ...$args);
}



use Krak\Invoke;

function hello($arg) {
    echo "Hello {$arg}\n";
}

$invoke = new Invoke\CallableInvoke();
$invoke->invoke('hello', 'World');



// some psr container
$container['service'] = function() {};
$invoke = Invoke\ContainerInvoke::create($container);
$invoke->invoke('service'); // will invoke the function returned from the container
$invoke->invoke('str_repeat', 'a', 10); // if not in container, will try to normally invoke



$container['service'] = function() { return new ArrayObject([1]); };
$invoke = Invoke\ContainerInvoke::createWithSeparator($container, '@');
$invoke->invoke('service@count'); // retrieves the service and invokes the count method which outputs 1 in this case



// this will invoke any object with the append method
$invoke = Invoke\MethodInvoke::create('append');

$data = new ArrayObject();
$invoke->invoke($data, 1);
$invoke->invoke($data, 2);
assert(count($data) == 2);