Please wait

Callable Type

The callable data type in PHP represents anything that can be called as a function. This provides a way to ensure that a function argument can be executed as a function or method.

A "callable" or "callback" is like a helper function that you give to another function to use. It's kind of like lending a friend a tool, and then they can use that tool when they need it.

Imagine you're baking a cake (the main task). You might have a friend help you by checking the oven temperature (a helper task). In PHP, baking the cake would be the main function, and checking the oven would be the callback function.

The callable type

PHP offers a type called callable to mark parameters that can hold functions. For example, take the following:

function run(callable $callback) {

In this example, we're type hinting the $callback parameter with the callable type. This forces the parameter to only accept functions as an argument value.

There are different ways of passing in a function. Any of the following can be considered callables.

  • String function name
  • Anonymous function
  • Invokable objects (We haven't talked about objects, so don't worry too much about this one.)
  • String static class method name (Same goes for this one.)
  • Callable array.

Let's go through some of these.

String function names

Firstly, we can pass in the function name with a string. For example, let's say we had a function called foo(). If we call the run() function, we can pass in the name like so:

function foo() {
  echo 'foo';
run('foo'); // 'foo'

Anonymous function

Secondly, we can pass in an anonymous function directly to the function like so:

run(function () {
  echo 'foo';
}); // 'foo'

Arrow function

Lastly, we can use an arrow function too:

run(fn foo() => print 'foo'); // 'foo'

Not allowed

If a parameter has the callable type, we can't use booleans, integers, or floats. However, we can use strings as long as the string contains the name of the function. Here are some invalid examples:

// All examples throw errors
run('Hello world');

Why use callables?

Using callable types and functions in PHP brings several benefits:

  • Reusability: By passing a function as a parameter (i.e., as a callable), you can reuse the same function in multiple places in your code, reducing duplication.
  • Abstraction: Callables can be useful in abstracting away implementation details. For instance, a function might require a sorting algorithm. Rather than hard-coding a specific algorithm, you can accept a callable that performs the sorting. This way, the function doesn't need to know the specifics of the sorting algorithm; it just needs to know that it can call the provided function to sort data.
  • Event-Driven Programming: In event-driven programming, you often need to specify which function should be called when an event occurs (like a mouse click in a user interface or data received from a network). Callables allow you to define these event handlers.
  • Functional Programming Paradigms: PHP isn't a functional programming language in the strict sense, but it does support many functional programming concepts. Callables, along with anonymous functions and closures, allow you to use techniques like higher-order functions (functions that take other functions as parameters or return them as results), which can make your code more expressive and easier to reason about.

Key takeaways

  • We can mark a parameter as the callable type.
  • We can pass in functions with strings, anonymous functions, or arrow functions.
  • Callables give us the power to execute code anytime within a function.


Please read this before commenting