[ACCEPTED]-What is a callback function and how do I use it with OOP-function-calls

Accepted answer
Score: 25

Here's a basic callback function example:

<?php

function thisFuncTakesACallback($callbackFunc)
{
    echo "I'm going to call $callbackFunc!<br />";
    $callbackFunc();
}

function thisFuncGetsCalled()
{
    echo "I'm a callback function!<br />";
}

thisFuncTakesACallback( 'thisFuncGetsCalled' );
?>

You 5 can call a function that has its name stored 4 in a variable like this: $variable().

So, in the above 3 example, we pass the name of the thisFuncGetsCalled function 2 to thisFuncTakesACallback() which then calls the function passed 1 in.

Score: 3

A callback function will use that function 8 on whatever data is returned by a particular 7 method.

I'm not sure how this particular 6 library works, but it could be something 5 as simple as:

$html = file_get_html('http://example.com');
$html->set_callback('make_bold');
$html->find('#title'); // returns an array

function make_bold($results) {
// make the first result bold
  return '<b>'.$results[0].'</b>';
}

ie, The function "make_bold()" will be 4 run on any data found. Again, I'm not sure 3 how this particular library works (ie, what 2 methods the callback function will get called 1 on)

Score: 3

A callback is either a function, an object 10 instance' method, or a static method on 9 a class. Either way, it's kind of a function 8 pointer. In some languages, functions are 7 a specific type. So you could assign a function 6 to a variable. These are generally called 5 function oriented languages. A good example 4 is Javascript.

In PHP, a callback can be 3 any of:

$fn = 'foo'; // => foo()
$fn = array($obj, 'foo'); // => $obj->foo()
$fn = array('Foo', 'bar'); // => Foo::bar()

See the manual entry for is_callable.

You can 2 invoke a callback with the rather verbose 1 function call_user_func.

Score: 2

Defination

A callbacks/callable is a simple function(either 9 it is anonymous or named function) that 8 we pass to another function as function 7 parameter which in the result returns that 6 passed function.

Example

function iWillReturnCallback($callBackHere){
    return $callBackHere;
}

function iAmCallBack(){
    echo "I am returned with the help of another function";
}

iWillReturnCallback(iAmCallBack());

//--Output -> I am returned with the help of another function

Don't be confused

There are some default functions 5 in php that accepts the name of the callback 4 function as a string in their parameter 3 because of avoiding conflicting between 2 the constant name and function name. So 1 don't be confused in these kind of things.

Score: 0

With PHP 5.3, you can now do this:

function doIt($callback) { $callback(); }

doIt(function() {
    // this will be done
});

Finally, a nice 2 way to do it. A great addition to PHP, because 1 callbacks are awesome.

Score: 0

The aim is to call a function that we want 2 eg: secretCode() but we want to use another function 1 as a helper or service to call it for us:

<?php
    
    // $call parameter can be anything
    function callBackServiceCenter($call)
    {
        echo "[callBackServiceCenter]: Hey, this is callBackServiceCenter function <br>We have received your command to call your requested function and we are now calling it for you! <br />";
        // Below is the part where it will call our secretCode()'s function
        $call();
        // And we can print other things after the secretCode()'s function has been executed:
        echo "[callBackServiceCenter]: Thank you for using our service at callBackServiceCenter. Have a nice day!<br />";
    }
    
    function secretCode()
    {
        echo "[secretCode]: Hey, this is secretCode function. Your secret code is 12345<br />";
    }
    
    callBackServiceCenter( 'secretCode' );
?>

Output:

[callBackServiceCenter]: Hey, this is callBackServiceCenter function
We have received your command to call your requested function and we are now calling it for you!
[secretCode]: Hey, this is secretCode function. Your secret code is 12345
[callBackServiceCenter]: Thank you for using our service at callBackServiceCenter. Have a nice day!

More Related questions