PHP Types Callable

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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);


Got any PHP Question?