Callback in PHP explained.

Bikram Tuladhar
2 min readNov 29, 2022

--

A callback is a function that gets passed into another function as an argument where it is executed when the parent function wants it to be executed. Here is a quick example of a callback function.

https://gist.github.com/bikramtuladhar/e1cf7ec8dc91c0f4f5241c5d3411de0f

Above the example, we are using a callable function in calculate the method where it accepts closure or function name and function argument as arguments. Calculate method extract callable functions and arguments using func_get_args(). ReflectionFunction is used to fit arguments and invoke the function.

https://gist.github.com/bikramtuladhar/46db0ea96e9324e6fdff9ed11002acba

Above the example, we are using call_usr_func to execute a callable function where it’s the first parameter is the function name and the rest is arguments. Using call_usr_func we can execute a function from another class too. Which is very handy in the case of refactoring code. Which is explained below in an example code snippet.

https://gist.github.com/bikramtuladhar/ffef9eccf880e57f80634e38ffc9a589

Above the example, we have three solutions for the same logic. In the first one, we are using closure to calculate the multiplication and we are writing all the logic inside the closure function. Alternatively, we have extracted the closure function to a normal PHP method name multiplyByTen which is called using the method name as a string. In the third approach, we have created a classCaland defined a multiply method to use in callback calls using the array[$cal, ‘multiply’] as an argument to execute code from class Cal. This array is treated as call_user_func_array.

Furthermore, here is a nice example of various ways to call a callable function https://www.exakat.io/the-art-of-php-callback.

Just like in JavaScript we can use a variable for function definition and execute dynamically in PHP as well.

https://gist.github.com/bikramtuladhar/139e9b1c7e13f6501d0ac1cbf8effcb0

In the example above, we have created three variables $multiply, $addtion and $operate as functions. These functions can be passed in callable which is executed accordingly.

--

--