WordPress.org

Make WordPress Core

Opened 3 months ago

Closed 2 months ago

#51900 closed enhancement (duplicate)

[PHP 8] Named parameters in hook `call_user_func_array()` calls

Reported by: ayeshrajans Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords: php8 close
Focuses: Cc:

Description

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

Change History (4)

#1 @SergeyBiryukov
3 months ago

  • Summary changed from [PHP 8] Named paramters in hook `call_user_func_array()` calls to [PHP 8] Named parameters in hook `call_user_func_array()` calls

#2 @jrf
3 months ago

Duplicate of task 3 of ticket #51553

#3 @ayeshrajans
3 months ago

  • Keywords close added; 2nd-opinion removed

You are right, it's better to track at #51553, my apologies about the noise.

#4 @jrf
2 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #51553.

Note: See TracTickets for help on using tickets.