The PHP callable or callback refers to the function that accepts another function callback in the parameter. Which is executing the code when calling the main function.

In this tutorial, we will focus on the user function callback. How to pass the function as a parameter to another function, execute the functions in the class, and so many other examples.

PHP Callable Overview

In the callable or the callback, we pass the function name into the user-defined function as a parameter. The user-defined functions can be like the below.

  • usort
  • call_user_func

There are also two other types of functions that can be passed into the user-defined function

  • Anonymous function
  • Arrow function

But Firstly, we need to understand what is the PHP function.

What is PHP Function

In general, the PHP function is a block that contains a lot of codes and it can be used many times in the track, it consists of two parts, the defined word in the PHP called function and beside it the name then the braces function func( $param ) then the function body or block between the curly braces { .. }. Let’s take an example.

<?php 
   function func( $param ) {
      // Your code here
   }
?>

The PHP Callable General Examples

The general example would be work, it should be like the below snippet code.

<?php 
   function func() {
      // Your code here
   }
   
   call_user_func( "func" );
?>

If the PHP function has parameters they should be passed to the user-defined function.

<?php 
   function func($param1, $param2) {
      // Your code here
   }
   
   call_user_func( "func", 55, 120 );
?>

In case the static function in the class, we should use the scope resolution operator ( :: ) Or an array contains the name of the class and the second field should be the name of the function, it should be in the string between the class name and the function name.

<?php 
   class Coded {
     public static function tag_calls () {
        echo "Hello Coded Tag ...";
     }
  }

  call_user_func( array( 'Coded', 'tag_calls' ) );
  
  // OR

  call_user_func( "Coded::tag_calls" );
?>

But if the function in the class already has a parameter, you must pass it to the user-define function as a second parameter.

<?php 
   class Coded {
     public function tag_calls ($param) {
        echo $param;
     }
  }

  call_user_func( array( 'Coded', 'tag_calls' ), "This is my world" );
?>

And with the class object variable

<?php 
   class Coded {
     public function tag_calls ($param) {
        echo $param;
     }
  }
  $obj = new Coded();
  call_user_func( array( $obj, 'tag_calls' ), "This is my world" );
?>

We can also use the user-defined function in a class function to call another one using the defined word that refers to instance ( $this )

<?php 
   class Coded {
     public function tag_calls ($param) {
        echo $param;
     }
     public function run() {
        call_user_func( array($this, "tag_calls"), "This is my world" );
     }
  }
  $obj = new Coded(); 
  $obj->run();
?>

Using Anonymous Function in PHP Callable

You just need to pass the function block into the user-defined function as a parameter, The code should look like the below.

<?php 

   call_user_func(function(){
     echo "Hello CodedTag Students !";
   }); 

?>

Using Arrow Function in PHP Callable

It is the same, you need to pass the arrow function in the user-defined function like the below example.

<?php 

   $some_calcs = fn( $x, $y ) => $x + $y;
   echo call_user_func($some_calcs, 125, 150); 

?>

Passing a Callback to a PHP Function

In this section, we will pass a function to another function using the defined data type word in the PHP ( callable ). It accepts the function as a parameter. Let’s take an example.

<?php 

   function exec1() {
     echo "CodedTag Students";
   }
   
   function run( callable $func ) { 
     return $func();
   }
   
   run( 'exec1' );

?>

Some Other Prefined Functions Using Callbacks in PHP

There are some other user-defined using callbacks in PHP listed below

  • array_walk
  • array_map
  • array_filter
  • call_user_func_array
  • usort

Conclusion

You understood how to use the user-defined callback with examples and how to use it with the PHP class. To see more examples check the PHP data Types tutorial.