PHP Callable


Example

Callables are anything which can be called as a callback. Things that can be termed a "callback" are as follows:

  • Anonymous functions

  • Standard PHP functions (note: not language constructs)

  • Static Classes

  • non-static Classes (using an alternate syntax)

  • Specific Object/Class Methods

  • Objects themselves, as long as the object is found in key 0 of an array

    Example Of referencing an object as an array element:

 $obj = new MyClass();
 call_user_func([$obj, 'myCallbackMethod']);

Callbacks can be denoted by callable type hint as of PHP 5.4.

$callable = function () {
    return 'value';
};

function call_something(callable $fn) {
    call_user_func($fn);
}

call_something($callable);