PHP code example of ascetik / callapsule

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

    

ascetik / callapsule example snippets


class Foo
{
    public function bar(){}
    public function __invoke(){}
    public static function biz(){}
}

// To wrap a Closure
$closureWrapper = CallWrapper::wrap(fn(string $name) => 'hello ' . $name);
// or
$closureWrapper = CallWrapper::wrapClosure(fn(string $name) => 'hello ' . $name);

// to wrap an instance and a method to call
$methodWrapper = CallWrapper::wrap([new Foo(), 'bar']);
// or
$methodWrapper = CallWrapper::wrapMethod(new Foo(), 'bar');

// to wrap a static method
$staticWrapper = CallWrapper::wrap([Foo::class, 'biz']);
// or
$staticWrapper = CallWrapper::wrapStatic(Foo::class, 'biz');

// To wrap an invokable class
$invokable = CallWrapper::wrap(new Foo());
// or
$invokable = CallWrapper::wrapInvokable(new Foo());



$callable = $closureWrapper->callable();
echo call_user_func($callable,' John'); // prints "hello John"

// or use it directly
echo $closureWrapper->apply(['name' => 'John']); // same result