PHP: Using Callback Function/method

Before going into the details syntax of using Callbacks in PHP, we need a use case where we can use callbacks. Many PHP build-in functions uses callback function and then we need to just pass our callbacks. WordPress also accepts callback at many places.
Here the example presented gives us clue about when we can use callback in PHP in our own functions. Suppose we download RSS feed from 10 sites and each sites takes 2 second in downloading RSS. So, ideally it will take 10*2 = 20 secs to download all rss files and then we will receive all data from the function.


<?php
function downloadRSS($sites) {
	$ret = array();
	foreach ($sites as $site) {
		// download rss for each site
		sleep(2); // supposed download time for each rss
		$ret[] = array(
				'site' => $site,
				'data' => 'downloaded data')
			);
	}
        return $ret;
}
// supposed sites for downloading rss
$sites = array (
	'http://site1.com',
	'http://site2.com'
);
$data = downloadRSS($sites);
foreach ($data as $row) {
	echo sprintf("Finished download %sn", $row['site']);
}
?>

Now, if we want to get rss data as soon as one site download is complete, we can write code in function itself, but that can create problem in reusability and if requirement changes.

<strong>Here is better solution using PHP's callback function:</strong>


<?php
function downloadRSS($sites, $callback = NULL) {
	$ret[] = array();
	foreach ($sites as $site) {
		sleep(2); // simulated downlaod code and time in each download
		$data = 'Downloaded data from each site.';
		if (is_callable($callback)) {
			call_user_func ($callback, $site, $data);
		}
	}
	$ret[] = array (
			'site' => $site,
			'data' => $data
		);
	return $ret;
}
// define functional class to use with callback
class sampleClass {
	public static function downloadComplete ($site, $data) {
		echo sprintf("Finished downloading from %sn", $site);
	}
}
//list of sites to download from
    $sites = array(
        'http://site1.com',
        'http://site2.com',
    );
$output =  downloadRSS($sites, array('sampleClass', 'downloadComplete'));
?>

For more detailed description of the above example with diagrams visit this blog. The example and code is taken from there.

In PHP, callbacks are used in various ways:


<?php
// An example callback function
function my_callback_function() {
    echo 'hello world!';
}
// An example callback method
class MyClass1 {
    static function myCallbackMethod1() {
        echo 'Hello World!';
    }
}
// An example callback method
class MyClass2 {
    static function myCallbackMethod2() {
        echo 'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');
// Type 2: Static class method call
call_user_func(array('MyClass1', 'myCallbackMethod1'));
// Type 3: Object method call
$obj = new MyClass2();
call_user_func(array($obj, 'myCallbackMethod2'));
// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass1::myCallbackMethod1');
// Type 5: Relative static class method call (As of PHP 5.3.0)
class A {
    public static function who() {
        echo "An";
    }
}
class B extends A {
    public static function who() {
        echo "Bn";
    }
}
call_user_func(array('B', 'parent::who')); // A
?>

More on callback's from PHP official site.

Comments are open for an year period. Please, write here on Facebook page.