Implementing Callback in PHP

In PHP, callbacks are functions that can be passed as arguments to other functions and executed at a later time. This powerful feature allows for flexible and reusable code design. PHP offers multiple ways to implement callbacks depending on your specific needs.

There are three common approaches to implement callbacks in PHP ?

  • Callback Functions

  • Anonymous Functions (Closures)

  • Callable Objects

Callback Functions

Callback functions are regular named functions that can be passed as arguments to other functions. The receiving function can then execute the passed function when needed.

Example

Here's an example of implementing callback functions in PHP ?

<?php
// Callback function example
function callbackFunction($arg1, $arg2) {
    echo "Callback function called with arguments: $arg1, $arg2<br>";
}

// Function that accepts a callback
function performOperation($callback, $arg1, $arg2) {
    echo "Performing operation...<br>";
    $callback($arg1, $arg2);
}

// Using the callback function
performOperation('callbackFunction', 'Hello', 'World');
?>
Performing operation...
Callback function called with arguments: Hello, World

In this example, callbackFunction is passed by name as a string to performOperation. The function is then invoked with the provided arguments, demonstrating how callbacks enable customizable behavior.

Anonymous Functions (Closures)

Anonymous functions, also known as closures, are functions without a specific name that can be defined inline. They are ideal for short, one-time operations.

Example

Here's an example of implementing anonymous functions in PHP ?

<?php
// Anonymous function example
$callback = function ($arg1, $arg2) {
    echo "Anonymous function called with arguments: $arg1, $arg2<br>";
};

// Function that uses the callback
function executeCallback($callback, $arg1, $arg2) {
    echo "Executing callback...<br>";
    $callback($arg1, $arg2);
}

// Using the anonymous function
executeCallback($callback, 'Hello', 'World');
?>
Executing callback...
Anonymous function called with arguments: Hello, World

Anonymous functions provide flexibility by allowing function definition on-the-fly without requiring a specific function name. They are commonly used with array functions like array_map() and array_filter().

Callable Objects

Callable objects are class instances that implement the __invoke() magic method, allowing them to be used as functions. This approach is useful when you need stateful behavior in your callbacks.

Example

Here's an example of implementing callable objects ?

<?php
class MyCallbackClass {
    private $prefix;
    
    public function __construct($prefix = "Callback") {
        $this->prefix = $prefix;
    }
    
    public function __invoke($arg1, $arg2) {
        echo "{$this->prefix} class called with arguments: $arg1, $arg2<br>";
    }
}

// Creating an instance of the callback class
$callbackObj = new MyCallbackClass("Custom");

// Using the callback object
$callbackObj('Hello', 'World');

// Can also be passed to other functions
function useCallable($callback, $arg1, $arg2) {
    $callback($arg1, $arg2);
}

useCallable($callbackObj, 'PHP', 'Callbacks');
?>
Custom class called with arguments: Hello, World
Custom class called with arguments: PHP, Callbacks

Callable objects provide object-oriented approach to callbacks, allowing you to maintain state and encapsulate related functionality within a class structure.

Comparison

Method Use Case State Management Reusability
Named Functions Simple callbacks No High
Anonymous Functions One-time operations Limited (via use) Medium
Callable Objects Complex stateful operations Yes High

Conclusion

PHP callbacks provide powerful ways to create flexible and reusable code. Choose named functions for simple callbacks, anonymous functions for inline operations, and callable objects when you need stateful behavior. Each approach offers different benefits depending on your specific requirements.

Updated on: 2026-03-15T10:32:48+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements