How to Implement a Callback Function in PHP

Kevin Amayi Feb 02, 2024
  1. Create a callback Function and Execute Using call_user_func in PHP
  2. Create a callback Function and Execute Using the array_map Method in PHP
  3. Implement Multiple callback Functions and Execute Them Using User-Defined Functions in PHP
  4. Implement the static Methods as callback Functions Using static Classes and call_user_func in PHP
How to Implement a Callback Function in PHP

This tutorial will show you how to create one or multiple callback functions and execute them using different built-in methods, user-defined functions and static classes in PHP.

Create a callback Function and Execute Using call_user_func in PHP

We create a callback function called testFunction() and execute it with the call_user_func() method by passing the function name as a string to the method.

Example:

<?php
    function testFunction() {
        echo "Testing Callback \n";
    }
    // Standard callback
    call_user_func('testFunction');
?>

Output:

Testing Callback

Create a callback Function and Execute Using the array_map Method in PHP

We execute the callback function with the array_map method. This will execute the method using the corresponding data passed to the array_map() function.

Example:

<?php
    function length_callback($item) {
      return strlen($item);
    }
    $strings = ["Kevin Amayi", "Programmer", "Nairobi", "Data Science"];
    $lengths = array_map("length_callback", $strings);
    print_r($lengths);
?>

Output:

Array ( [0] => 11 [1] => 10 [2] => 7 [3] => 12 )

Implement Multiple callback Functions and Execute Them Using User-Defined Functions in PHP

We’ll execute two callback functions called name and age using a user-defined function called testCallBacks(), bypassing the function’s names as strings to the user-defined function.

Example:

<?php
function name($str) {
  return $str . " Kevin";
}

function age($str) {
  return $str . " Kevin 23 ";
}

function testCallBacks($str, $format) {
  // Calling the $format callback function
  echo $format($str)."<br>";
}

// Pass "name" and "age" as callback functions to testCallBacks()
testCallBacks(" Hello", "name");
testCallBacks(" Hello", "age");
?>

Output:

Hello Kevin
Hello Kevin 23

Implement the static Methods as callback Functions Using static Classes and call_user_func in PHP

We’ll create two static classes with the static method and execute them as callbacks using the call_user_func() method.

<?php
    // Sample Person class
    class Person {
          static function walking() {
              echo "I am moving my feet <br>";
          }
      }
    //child class extends the parent Person class
    class Student extends Person {
      static function walking() {
            echo "student is moving his/her feet <br>";
        }
    }
    // Parent class Static method callbacks
    call_user_func(array('Person', 'walking'));
    call_user_func('Person::walking');

    // Child class Static method callback
    call_user_func(array('Student', 'Student::walking'));
?>

Output:

I am moving my feet
I am moving my feet
student is moving his/her feet