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

26,347

Solution 1

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.

Solution 2

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)

Solution 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.

Share:
26,347
Paul M
Author by

Paul M

Web developer trying to self learn everything!!

Updated on July 05, 2022

Comments

  • Paul M
    Paul M almost 2 years

    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!

  • Tim Visée
    Tim Visée over 10 years
    Great and simple answer, thanks. I've one question though, what is the minimum required PHP version that has this feature available?
  • Sophia_ES
    Sophia_ES over 8 years
    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.php
  • Spartacus
    Spartacus over 6 years
    What happens if $callback is not a function? Does PHP throw a fatal error, warning, or nothing?