PHP code example of henzeb / closure

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

    

henzeb / closure example snippets


use function Henzeb\Closure\closure;

class InvokableClass
{
    public function __invoke() {
        print 'Hello World!';
    }
}

class ReturnsClosures
{
    public function __invoke(): Closure {
        print fn()=>'Hello World!';
    }
}

class NotInvokableClass
{
    public function invoke() {
        print 'Hello World!';
    }
}

function helloWorld()
{
    print 'Hello World!';
}

closure(Invokable::class)(); // prints Hello World!
closure(new InvokableClass)(); // prints Hello World!

closure(ReturnsClosure::class)(); // prints Hello World!
closure(new ReturnsClosure())(); // prints Hello World!

closure('helloWorld')(); // prints Hello World!

closure(
    function(){
        print 'Hello World!'
    }
); // prints Hello World!;

closure(NotInvokable::class); // throws TypeError

use function Henzeb\Closure\closure;

class InvokableClass
{
    public function __construct(private $message)
    {
    }

    public function __invoke() {
        print $this->message;
    }
}

closure(
    Invokable::class,
    fn(string $class) => $class('Hello World!')
); // prints Hello World!

use function Henzeb\Closure\bind;

bind(
    fn()=> // do what you need
    , $anyThis
)('your Arg');

class InvokableClass {
    public function __invoke($hello): Closure {
        return $anyThis->hello($hello);
    }
}

use function Henzeb\Closure\bind;

bind(InvokableClass::class, $anyThis)('your Arg');
bind(InvokableClass::class, $anyThis, AnyScope::class)('your Arg');

bind(new InvokableClass, $anyThis)('your Arg');

use function Henzeb\Closure\call;

call(
    fn()=>'Hello world!'
); // returns Hello World!

call(
    InvokableClass::class
); // returns whatever your callable returns

call(InvokableClass::class, $anyThis); // returns whatever your callable returns
call(
    InvokableClass::class,
    $anyThis,
    AnyScope::class
); // returns whatever your callable returns
call(new InvokableClass, $anyThis);  // returns whatever your callable returns

call(
    InvokableClass::class,
    $anyThis,
    resolve: fn($class) => new $class('Hello World!')
); // returns whatever your callable returns

call(
    fn()=>'Hello world!'
    , $anyThis
); // returns Hello World!

class NonInvokable {
    public function hello()
    {
        print 'Hello World!';
    }
}

use function Henzeb\Closure\closure;
use function Henzeb\Closure\bind;
use function Henzeb\Closure\call;

closure(NonInvokable::class, invoke: 'hello')(); // prints Hello World!;
bind(NonInvokable::class, $newthis, invoke: 'hello')(); // prints Hello World!;
call(NonInvokable::class, $newthis, invoke: 'hello'); // prints Hello World!;


use function Henzeb\Closure\invokable;

invokable(NonInvokable::class); // returns false
invokable(NonInvokable::class, 'hello'); // returns true

invokable(InvokableClass::class); // returns true
invokable([]); // returns false
invokable(STDIN); // returns false


use function Henzeb\Closure\wrap;

wrap(NonInvokable::class)(); // returns NonInvokable instance

wrap(true)(); // returns true
wrap(false)(); // returns true

wrap(InvokableClass::class)(); // returns what __invoke would return

wrap(
    InvokableClass::class,
    invoke: 'invokableMethod'
)(); // returns what invokableMethod would return

wrap([])(); // returns an empty array
wrap(STDIN)(); // returns the STDIN stream


use function Henzeb\Closure\binding;

binding(function(){})->getScope(); // returns $newScope value
binding(function(){})->getThis(); // returns $newThis value


use function Henzeb\Closure\binding;

$myVariable = 'Hello World!';

$closure = function() use ($myVariable) {
    static $myStaticVariable;

    $myStaticVariable = 'Hello World!'
}

binding($closure)->get('myVariable'); // returns Hello World!
binding($closure)->get('myOtherVariable'); // returns null.

binding($closure)->get('myStaticVariable'); // returns null.

$closure(); //calling the closure

binding($closure)->get('myStaticVariable'); // returns Hello World!



use function Henzeb\Closure\binding;

var_dump(binding(fn()=>true));