axy/callbacks

Extension of php-callback format

Installs: 126

Dependents: 4

Stars: 1

Watchers: 2

Open Issues: 1

Language: PHP

1.0.2 2015-03-31 10:58 UTC

README

PHP 5.4+

Library does not require any dependencies (except composer packages).

Documentation

Examples

function sum($a, $b)
{
    return $a + $b;
}

Standard callback:

$callback = new Callback('sum');

echo $callback(2, 2);

Binging arguments:

$callback = new Callback('sum', [3]);

echo $callback(4); // 3 + 4 = 7

Binging context:

class MyClass
{
    public function getEventHandler()
    {
        return new Callback([$this, 'onEvent'], ['click'], true);
    }

    private function onEvent($event)
    {
        echo 'Event '.$event.'!';
    }
}

$obj = new MyClass();
$handler = $obj->getEventHandler();

// click
$handler(); // "Event click!". Private method was called