Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Callback Function in PHP?
3.
Passing Callback
3.1.
Simple callback
3.2.
PHP
3.3.
Static class method call
3.4.
PHP
3.5.
Object method callback
3.6.
PHP
3.7.
PHP
3.8.
Callback using a Closure
3.9.
PHP
4.
Frequently Asked Questions
4.1.
What is the type of callback function in PHP?
4.2.
What is the callback function used for?
4.3.
What is another name for callback function?
4.4.
What is the difference between callback function and regular function?
5.
Conclusion
Last Updated: Jun 5, 2024
Easy

PHP callback function

Author Ranjul Arumadi
2 upvotes

Introduction

There are some cases when we want a function not to run before a particular task is completed, but we want to run it right after the job has been completed. This is where the callback function comes into play. 

PHP callback function

A callback function is a function that is supplied as an argument to another function and ensures that it is only executed when a specific task has been completed.

What is Callback Function in PHP?

We can use the callable type declaration to indicate PHP callbacks functions.

Any existing function may be used as a PHP callback function. Pass a string containing the function’s name as an argument to another function to use it as a callback function.

Passing Callback

There are four types of callbacks in PHP. They are:

Simple callback

In PHP, we can use the call_user_func() function to call functions, where arguments are the function's string name.

 

Example:

  • PHP

PHP

<?php

// Function to callback
function my_callback_function() {
echo 'Hello, world!';
}

// PASS my_callback_function INTO call_user_func()

call_user_func('my_callback_function');
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

Hello, world!

 

  • We pass my_callback_function() into call_user_func() as a parameter.
  • The callback function is called from within the function. call_user_func() will call my_callback_function().

Static class method call

We can call static class methods by passing the class name instead of an object at index 0 or passing 'ClassName::methodName' without creating an object of that class.

Example:

  • PHP

PHP

<?php

// An example static callback method

class MyClass {
static function myCallbackMethod() {
echo 'Hello World!'.'<br>';
}
}

// Static class method callback
call_user_func(array('MyClass', 'myCallbackMethod'));

// Static class method callback
call_user_func('MyClass::myCallbackMethod');
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

Hello World!
Hello World!


If we need to callback a function in a class, this is the way to go. However, the callback function must be static, else PHP will return an error.

Must Read PHP Projects With Source Code

Object method callback

We can use the call_user_func() function to call object methods, with the parameter being an array containing the object variable and the string name of the method to call.

 

Example:

  • PHP

PHP

<?php
// A sample class
class MyClass {
static function myCallbackMethod() {
echo 'Hello World!';
}
}

// Object method callback
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));
?>
You can also try this code with Online PHP Compiler
Run Code

 

Output:

Hello World!

Objects implementing __invoke can be used as callables. Object methods can also be called if the __invoke() function declaration is used to make them invokable. In this situation, the object variable is the argument to the call_user_func() method.

 

Example:

  • PHP

PHP

<?php
class obj {
public function __invoke($name) {
echo 'Hello ', $name, "\n";
}
}

$classObjVar = new obj();
call_user_func($c, 'PHP!');
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

Hello PHP!

Callback using a Closure

We can call closure functions via conventional calls or map the closure function to an array of valid arguments using the array_map() function. The arguments are the closure function and an array of valid arguments.

Example:

  • PHP

PHP

<?php
// Our closure
$double = function($obj) {
return $obj ** 2;
};

// This is our range of numbers
$number = range(1, 10);

// Use the closure as a callback to get the square of each element in our range
$square = array_map($double, $number);

print implode(' ', $square);
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

1 4 9 16 25 36 49 64 81 100

 

 


Also see - Difference between argument and parameter

Frequently Asked Questions

What is the type of callback function in PHP?

A callback function in PHP can be a string, array, or anonymous function (closure) that references a function or method to be executed later.

What is the callback function used for?

Callback functions are used for deferred execution, such as event handling, array manipulation functions, or asynchronous processing, allowing code to be more flexible and modular.

What is another name for callback function?

Another name for a callback function is a "call-after" or "hook," indicating it is called after a certain event or condition is met.

What is the difference between callback function and regular function?

A regular function is called directly, whereas a callback function is passed as an argument and invoked by another function at a later time or event.

Conclusion

A callback function is a function that is supplied as an argument to another function and ensures that it is only executed when a specific task has been completed. We have looked at the types of callback functions in PHP and also saw some code examples regarding the same.

Live masterclass