[wp-trac] [WordPress Trac] #51900: [PHP 8] Named paramters in hook `call_user_func_array()` calls

WordPress Trac noreply at wordpress.org
Tue Dec 1 00:03:59 UTC 2020


#51900: [PHP 8] Named paramters in hook `call_user_func_array()` calls
-------------------------+------------------------------
 Reporter:  ayeshrajans  |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:
 Severity:  normal       |   Keywords:  php8 2nd-opinion
  Focuses:               |
-------------------------+------------------------------
 PHP 8.0 is here, and it comes with named parameters.

 While named parameter is a new feature that syntax would simply not work
 in older PHP versions, [https://php.watch/versions/8.0/named-parameters
 #named-params-call_user_func_array `call_user_func_array function behaves
 differently].

 WordPress uses `call_user_func_array` extensively throughout the code,
 including filters and actions. With PHP 8.0, if the second parameter
 (`$param_arr`) is an associative array, PHP tries to call the callable as
 a named parameter.

 See example: https://3v4l.org/RhjrU

 What this means for WordPress, is that all parameters that a registered
 action, filter, or a standard "params" value **must not be an associative
 array**. This behavior is not warned. If the parameter array keys match
 function parameters, that is mostly a safe call, but if there is at least
 one parameter name that does not match, PHP will then raise a fatal error.


 One easy way to "normalize" the parameters is to call `array_values`
 before calling the function.

 Before submitting any patches, I would like to request opinions if this is
 desirable for WordPress. I guess reports will come in once users start to
 use PHP 8.0, and it's probably an easier fix at `call_user_func_array()`
 call-site rather than the action/filter/params declaration.

 Disclosure: I am the author of the page linked at the top of this ticket;
 I believe it provides more context without typing the whole story.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51900>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list