What are callbacks
Callback is a piece of code that is passed to some other functions as a parameter, such that it can be executed before or after a certain event or change of state. A common example of callbacks can be seen in javascript when using the jquery ajax function.
$.ajax({ url: 'ajax/test.html', success: function(data) { $('.result').html(data); alert('Load was performed.'); } });
The success index has a value, that is a function. And it runs after the ajax query completes successfully. Its more of an event handler, that is implemented through callbacks.
Callbacks in PHP
As seen above, callbacks require passing functions as an argument. Prior to version 5.3 callbacks were implemented in PHP using call_user_func.
echo "Hello there. We are ready to do the tasks..."; task('happy' , 'my_callback_function'); function task($parameters , $callback = false) { //Do the tasks //...task over //Now call the callback if($callback !== false) { call_user_func($callback); } } // An example callback function function my_callback_function($something) { echo 'hello world!'; }
In the above example, the task function is called with an optional parameter for a callback function. Any function can be provided as a callback function and that would be executed, after the task function finishes its tasks.
Lets take a more realistic example which uses classes and objects. Once I was writing a piece of code like this
class Profile { function update_profile() { //get $_POST data //... //get uploaded photo $f = new FileUpload('photo'); $f->save(); //RESIZE the photo, where to add the code for resizing ?????? } }
The FileUpload class is a generic one that would manage uploading and saving the file to the correct location. It is not supposed to do anything more. However over here the uploaded file was actually a photo, which had to be resized after saving. Now there are multiple ways to do so.
1. Get the saved path of the file from the FileUpload object and do the resizing in the update_profile function. 2. Extend the FileUpload class and override the save method, adding code for resizing. 3. Make a provision for a callback function to do some processing after the upload.
The above use case demonstrates one significant use of callbacks that is to add functionality to existing class without having to extend it. For this ofcourse the class needs to have provision for accommodating callbacks.
Inheritance is not always the best way to add/modify functionality of existing classes. In the above example the FileUpload class would have to be extended everytime it is required to do some additional processing.
So callbacks provide a simple way to add functionality to the FileUpload class. Since version 5.3 PHP supports anonymous functions and closures.
The original FileUpload class has to be modified to accomodate a callback.
class FileUpload { function save($callback) { $path = '/path/image'; echo "Data saved<br />"; //call the callback $callback($path); } }
Other classes can now use callbacks with FileUpload class
class Profile { function update_profile() { $f = new FileUpload('photo'); // Note, this syntax is very similar to javascript callback syntax $f->save(function($path){ echo "Image resized at path $path <br />"; }); } } $p = new Profile; $p->update_profile();
The simple example shows how anonymous functions can be used to implement callbacks easily and how callbacks can be used to add functionality to existing classes without inheritance.
The function being passed as a callback parameter does not have a name, hence is called anonymous function (called lambda function in python).
I had to add a variable for my_callback_function($something) to work:
call_user_func($callback,”the something”);
Fantastic explanation, beautifully simple. Thanks!!!