What is a callback function and how do I use it with OOP
Asked Answered
P

6

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!

Petula answered 23/10, 2008 at 20:19 Comment(0)
S
26

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.

Seasickness answered 23/10, 2008 at 20:22 Comment(2)
Great and simple answer, thanks. I've one question though, what is the minimum required PHP version that has this feature available?Gamo
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: php.net/manual/en/function.call-user-func.phpJolie
G
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)

Guileless answered 23/10, 2008 at 20:25 Comment(0)
G
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.

Groveman answered 23/10, 2008 at 22:53 Comment(0)
A
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.

Antabuse answered 23/10, 2008 at 20:20 Comment(0)
K
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.

Kaleykaleyard answered 14/11, 2017 at 6:45 Comment(1)
What happens if $callback is not a function? Does PHP throw a fatal error, warning, or nothing?Praenomen
S
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!
Socher answered 6/1, 2022 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.