[wp-trac] [WordPress Trac] #14671: Deprecate the "accepted args" argument in add_filter() and add_action()

WordPress Trac wp-trac at lists.automattic.com
Thu Jul 14 20:56:24 UTC 2011


#14671: Deprecate the "accepted args" argument in add_filter() and add_action()
-------------------------+-----------------------------
 Reporter:  markjaquith  |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Future Release
Component:  Plugins      |     Version:
 Severity:  normal       |  Resolution:
 Keywords:  has-patch    |
-------------------------+-----------------------------
Changes (by 5ubliminal):

 * cc: wp-trac@… (added)


Comment:

 Nobody should use stock PHP functions in WP actions/filters. But OMG...
 they might do. Reflection should be the solution. See sample:


 {{{
 function forward_call_reflection($function){
         if(!is_callable($function)) return false; // Make sure it's worth
 continuing
         $args = func_get_args(); array_shift($args); // Get arguments,
 shift function (1st arguments)
         // No Reflection, bail here... false is to prevent autoload
         if(!class_exists('ReflectionFunction', false)){
                 return call_user_func_array($function, $args);
         }
         // Now gain access to reflection object and continue
         $refunc = new ReflectionFunction($function);
         // Call user defined functions directly as they don't care about
 argument counts
         if($refunc->isUserDefined()){
                 return call_user_func_array($function, $args);
         }
         // Now let's be cautious about arguments
         $this_argc = count($args); // Arguments fed to this function
         $argc = $refunc->getNumberOfParameters(); // Function arguments
 count (including predefined optionals)
         $req_argc = $refunc->getNumberOfRequiredParameters(); // Required
 arguments (with no default values)
         while($this_argc++ < $req_argc) $args[] = null; // Push NULL to
 fill gaps
         if($this_argc > $argc) array_splice($args, $argc); // POP extra
 arguments
         return $refunc->invokeArgs($args); // Invoke function
 }
 }}}

 Performance penalty is double the processing time.
 100 calls to addslashes() with no Reflection take 0.0005870 while 100
 calls with Reflection take 0.0013950.

 It's unnoticeable IMO.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/14671#comment:19>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list