Passing functions as call by ref parameter should be avoided
|Reported by:||TobiasBg||Owned by:||westi|
I recently had a bug report for a plugin, where script execution was stopped at an avoidable warning.
The problem was a call similar to
$my_array = array( ... ); $last_element_index = array_pop( array_keys( $my_array ) );
The reason for the issue is that the parameter for array_pop() is passed as a call by reference parameter. This leads to a problem here, because array_keys( $my_array ) is not a variable that could be changed during the call by reference process (in this case, the last element can not be removed from the array).
This raises a warning by PHP (also documented at www.php.net/array_pop), and there are hosts that seem to be stopping PHP execution at such warning.
Fixing the issue is very easy by adding a variable:
$my_array = array( ... ); $_my_array_keys = array_keys( $my_array ); $last_element_index = array_pop( $_my_array_keys );
As I had this issue in my plugin, I also grep'ed WP core and found two instances of array_pop( array_keys() ):
- admin-ajax.php (line 893)
- ms-edit.php (line 107)
With array_pop( explode() ):
- wp-app.php (line 226)
With array_pop( split() ):
- atomlib.php (lines 151 and 230)
There are more functions (especially within array_*) that are affected, and that should be checked on whether a function call is passed where a call by ref parameter is expected.
Change History (21)
- Component changed from General to Warnings/Notices
- Owner set to westi
comment:17 nacin — 3 years ago
- Keywords 3.2-early added; early removed
- Milestone changed from 3.1 to Future Release