Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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.

share|improve this answer
1  
Thanks @hakre! This is exactly why I am keen on sticking to PHP 5.3. I love progress and I wish more people would embrace it. –  TTL Dec 23 '10 at 8:29

http://php.net/manual/en/keyword.class.php

Caution
PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

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

share|improve this answer
1  
Nah, the issue seems to be that @TTL uses magic method (exactly as intended), but that method was only added in PHP 5.3 –  Rarst Dec 22 '10 at 13:43
    
Exactly, thanks @Rarst! __callStatic was added in 5.3 –  TTL Dec 22 '10 at 13:47
    
Also, thanks @t31os for the answer! –  TTL Dec 22 '10 at 13:58
    
@TTL - No worries, always happy to share my thoughts... :) –  t31os Dec 22 '10 at 14:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.