I have a class that handles AJAX requests for my plugin. The class has a __callStatic wrapper for all actions, which subsequently calls non-static methods for each action, like so:
class TP_AJAX_wrapper {
public static function __callStatic( $name, $args = null ) {
check_ajax_referer( 'tp_ajax_nonce' );
$out = array();
try {
$res = call_user_func( array( __CLASS__, $name ) );
} catch(Exception $e) {
$out['error'] = $e->getMessage();
}
$out['responseStatus'] = $res ? 'ok' : 'null';
$out['response'] = $res;
echo json_encode($out);
die();
}
protected static function someAction() {
return array( 'someValue' => 3 );
}
}
Then I have an array of actions which I initialize using:
foreach ( $ajax_actions as $action ) {
add_action ( 'wp_ajax_tp_'.$action, array( 'TP_AJAX_wrapper', $action ) );
}
This works great for PHP 5.3+, but fails miserably on older versions.
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, ‘TP_AJAX_wrapper::someAction’ was given in /path/to/wordpress/wp-includes/plugin.php on line 395
Since it’s written in a plugin, used on all sorts of hosts, some of which support PHP 5.3, others which don’t, I have to make it a little more flexible. What I can’t figure out is a way to emulate __callStatic (introduced in 5.3) for older versions of PHP.
What I’m looking for is a way to emulate the __callStatic wrapper for older versions. I’ve tried using __call as well, which is supposed to handle static method calls if the first argument is a classname, rather than an object, but I keep getting the same error.
Halp?
There is no
__callStatic
magic function in PHP < 5.3. As so, it can not be identified as callback.As WordPress does not validate prior to invoke a callback with filters and hooks, you get the PHP warning because running on PHP < 5.3 it is not a valid callback.
You can change the design of your callbacks to be non-static and make use of the
__call
magic function which is available with earlier PHP 5 versions if you’re looking for a similar feature.But sticking to PHP 5.3 is not that bad.
http://php.net/manual/en/keyword.class.php
I don’t know a huge deal about classes, but it might be the name spacing you’re using for the function(s) causing a problem.
I could be wrong of course..