12

I want to use the php simple HTML DOM parser to grab the image, title, date, and description from each article on a page full of articles. When looking at the API I notice it has a set_callback which Sets a callback function. However im not sure what this does or how I would use it? In one of the examples its used to call a function which strips out some stuff, im wondering if you have to use this to call all functions?

I guess im wondering why I use this, and what does it do as I have never come across a callback function before!

Paul M
  • 3,937
  • 9
  • 45
  • 53

6 Answers6

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 can call a function that has its name stored in a variable like this: $variable().

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

Mark Biek
  • 146,731
  • 54
  • 156
  • 201
  • Great and simple answer, thanks. I've one question though, what is the minimum required PHP version that has this feature available? – Tim Visée Mar 09 '14 at 13:55
  • 4
    This answer is out-of-date. Today, PHP uses the term 'callback' to refer to a specific kind of function. For more information read PHP's own documentation on the matter: http://php.net/manual/en/function.call-user-func.php – Sophia_ES Nov 25 '15 at 14:36
3

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

I'm not sure how this particular library works, but it could be something 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 run on any data found. Again, I'm not sure how this particular library works (ie, what methods the callback function will get called on)

Owen
  • 82,995
  • 21
  • 120
  • 115
3

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

In PHP, a callback can be 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 invoke a callback with the rather verbose function call_user_func.

troelskn
  • 115,121
  • 27
  • 131
  • 155
2

Defination

A callbacks/callable is a simple function(either it is anonymous or named function) that we pass to another function as function parameter which in the result returns that 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 in php that accepts the name of the callback function as a string in their parameter because of avoiding conflicting between the constant name and function name. So don't be confused in these kind of things.

0

With PHP 5.3, you can now do this:

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

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

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

S.I.
  • 3,250
  • 12
  • 48
  • 77
0

The aim is to call a function that we want eg: secretCode() but we want to use another function 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!
MaXi32
  • 632
  • 9
  • 26