Make WordPress Core

Opened 14 months ago

Closed 13 months ago

Last modified 11 months ago

#63853 closed enhancement (fixed)

Add PHP 8.5 compat array functions: `array_first` and `array_last`

Reported by: TobiasBg Owned by: jorbin
Priority: normal Milestone: 6.9
Component: General Version:
Severity: normal Keywords: php85 has-patch has-unit-tests commit has-dev-note
Cc: Focuses: php-compatibility

Description

PHP 8.5, expected to be released on November 20, 2025, thus shortly before WP 6.9, will bring two new array functions: array_first and array_last.

See ​https://wiki.php.net/rfc/array_first_last

Following up on [59783] (for #62558), [57337], and [52038], we should include polyfills in Core, to make developer adoption easier. For more reasoning, see ticket:62558#comment:6 .

Further details as well as possible polyfills are available at ​https://php.watch/versions/8.5/array_first-array_last and in the ​polyfills/array-first-array-last package.

Change History (15)

#1 @tusharbharti
14 months ago

Hi, I am interested in creating a compat for array_first and array_last but would like to confirm how the function should handle following cases:

  • If the values passed to the parameters are empty-ish like null, 0 , false etc, should the function return null ( by allowing empty-ish value to be passed ) or throw error when trying to access the key for these.
  • If any error occurs while trying to get the first/last key of the array or trying to get the value from the array, should the function handle these errors or this is left for consumers to handle.
Last edited 14 months ago by tusharbharti (previous) (diff)

#2 @peterwilsoncc
14 months ago

@tusharbharti Thank you, that would be very helpful.

If the values passed to the parameters are empty-ish like null, 0 , false etc, should the function return null ( by allowing empty-ish value to be passed ) or throw error when trying to access the key for these.

In PHP the new functions are type strict so the compatibility implementations should be too. If you define the functions as function array_*( array $array ) then that will trigger the fatal error.

If any error occurs while trying to get the first/last key of the array or trying to get the value from the array, should the function handle these errors or this is left for consumers to handle.

Being type strict, this should be a use case that needs to be covered.

#3 @tusharbharti
14 months ago

Oh, got it! I will then start implementing the compat.

This ticket was mentioned in ​PR #9556 on ​WordPress/wordpress-develop by ​@tusharbharti.


14 months ago
#4

  • Keywords has-patch has-unit-tests added; needs-patch removed

Add polyfills for array_first() and array_last(), planned for PHP 8.5. These provide a safe, side-effect-free way to fetch the first and last values of an array. This forward-compatibility makes it easier for Core, plugins, and themes to adopt upcoming PHP features while maintaining support for older versions.

The PR includes the polyfills for both functions in compat.php as well as unit tests.

Trac ticket: https://core.trac.wordpress.org/ticket/63853

​@tusharbharti commented on ​PR #9556:


14 months ago
#5

Hi, I have a question ( not related to ticket but related to tests). Is there a guidelines on how tests comments are to be written as when I checked the tests files for PHPUnit, some files have comments as well as ticket mentioned but others either don't have any comments nor the tickets.

​@peterwilsoncc commented on ​PR #9556:


14 months ago
#7

Is there a guidelines on how tests comments are to be written

It's a bit of mixed bag but something contributors are working toward improving. It's become relatively common for contribuitors to use the order listed in the description of this trac ticket.

​@tusharbharti commented on ​PR #9556:


14 months ago
#8

It's a bit of mixed bag but something contributors are working toward improving. It's become relatively common for contribuitors to use the order listed in the description of this trac ticket.

Oh, got it! 👍, I will follow this format from now on.
Is there anything else that I need to implement from my side on this?

#9 @mukesh27
14 months ago

  • Keywords commit added

Mark ready for commit

#10 @jorbin
13 months ago

  • Owner set to jorbin
  • Status new → assigned

Self assigning for final review

#11 @jorbin
13 months ago

  • Resolution → fixed
  • Status assigned → closed

In 60672:

General: Add polyfills for new PHP 8.5 array functions: array_first and array_last.

This power couple of function is coming in PHP 8.5: array_first and array_last.

For more information on these functions, check out the PHP RFC at ​https://wiki.php.net/rfc/array_first_last.

Props tusharbharti, jorbin, peterwilsoncc, mukesh27, johnbillion.
Fixes #63853. See #63061.

#12 @Soean
13 months ago

In array_first() we could use

return $array[ array_key_first( $array ) ];

instead of

foreach ( $array as $value ) {
        return $value;
}

#13 @TobiasBg
13 months ago

@Soean: I think this was a deliberate choice to save a function call, see ​https://github.com/WordPress/wordpress-develop/pull/9556#discussion_r2289844485
It's the same functionality, minus a otherwise-duplicated empty() check, see ​https://github.com/WordPress/wordpress-develop/blob/6.8.2/src/wp-includes/compat.php#L310-L312

#14 @johnbillion
13 months ago

PHP also does this internally:

ZEND_HASH_FOREACH_VAL(array, zval *zv) {
	RETURN_COPY_DEREF(zv);
} ZEND_HASH_FOREACH_END();

#15 @desrosj
11 months ago

  • Keywords has-dev-note added

These new polyfill functions were detailed in the miscellaneous dev note for developer-focused changed: ​https://make.wordpress.org/core/2025/11/17/miscellaneous-developer-focused-changes-in-6-9/

Note: See TracTickets for help on using tickets.