Callbacks

Callbacks can be denoted by callable type hint as of PHP 5.4. This documentation used callback type information for the same purpose.

Some functions like call_user_func() or usort() accept user-defined callback functions as a parameter. Callback functions can not only be simple functions, but also object methods, including static class methods.

Passing

A PHP function is passed by its name as a string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo, empty(), eval(), exit(), isset(), list(), print or unset().

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object at index 0. As of PHP 5.2.3, it is also possible to pass 'ClassName::methodName'.

Apart from common user-defined function, anonymous functions can also be passed to a callback parameter.

Example #1 Callback function examples

<?php 

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}

// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}

// Type 1: Simple callback
call_user_func('my_callback_function'); 

// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod')); 

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));

// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');

// Type 5: Relative static class method call (As of PHP 5.3.0)
class {
    public static function 
who() {
        echo 
"A\n";
    }
}

class 
extends {
    public static function 
who() {
        echo 
"B\n";
    }
}

call_user_func(array('B''parent::who')); // A
?>

Example #2 Callback example using a Closure

<?php
// Our closure
$double = function($a) {
    return 
$a 2;
};

// This is our range of numbers
$numbers range(15);

// Use the closure as a callback here to 
// double the size of each element in our 
// range
$new_numbers array_map($double$numbers);

print 
implode(' '$new_numbers);
?>

The above example will output:

2 4 6 8 10

Note: In PHP 4, it was necessary to use a reference to create a callback that points to the actual object, and not a copy of it. For more details, see References Explained.

Note:

Callbacks registered with functions such as call_user_func() and call_user_func_array() will not be called if there is an uncaught exception thrown in a previous callback.

add a note add a note

User Contributed Notes 6 notes

up
17
steve at mrclay dot org
1 year ago
Performance note: The callable type hint, like is_callable(), will trigger an autoload of the class if the value looks like a static method callback.
up
10
andrewbessa at gmail dot com
2 years ago
You can also use the $this variable to specify a callback:

<?php
class MyClass {

    public
$property = 'Hello World!';

    public function
MyMethod()
    {
       
call_user_func(array($this, 'myCallbackMethod'));
    }

    public function
MyCallbackMethod()
    {
        echo
$this->property;
    }

}
?>
up
4
computrius at gmail dot com
10 months ago
When specifying a call back in array notation (ie. array($this, "myfunc") ) the method can be private if called from inside the class, but if you call it from outside you'll get a warning:

<?php

class mc {
   public function
go(array $arr) {
      
array_walk($arr, array($this, "walkIt"));
   }

   private function
walkIt($val) {
       echo
$val . "<br />";
   }

    public function
export() {
        return array(
$this, 'walkIt');
    }
}

$data = array(1,2,3,4);

$m = new mc;
$m->go($data); // valid

array_walk($data, $m->export()); // will generate warning

?>

Output:
1<br />2<br />3<br />4<br />
Warning: array_walk() expects parameter 2 to be a valid callback, cannot access private method mc::walkIt() in /in/tfh7f on line 22
up
2
metamarkers at gmail dot com
1 year ago
you can pass an object as a callable if its class defines the __invoke() magic method..
up
-1
Yzmir Ramirez
4 months ago
> As of PHP 5.2.3, it is also possible to pass 'ClassName::methodName'

You can also use 'self::methodName'.  This works in PHP 5.2.12 for me.
up
-10
raj dot aditya at gmail dot com
4 months ago
i did the same thing mentioned above but the following code not working. Why?
---------------------------------------------------------
class Alfa1 {
    public static function falfa() {
        echo "In Alfa";
    }
}

class Beta1 extends Alfa1 {
    public static function falfa() {
        echo "In Beta";
    }
}

call_user_func(array('Beta1','parent::flafa'));

Tested in two latest version of PHP both gives:

Warning: call_user_func() expects parameter 1 to be a valid callback, class 'Alfa1' does not have a method 'flafa' in Xampp\htdocs\a.php on line 15
To Top