#45055 closed feature request (fixed)
Introduce new PHP cross-version compatibility functions, `array_key_first()`, `array_key_last()`
Reported by: | desrosj | Owned by: | hellofromTonya |
---|---|---|---|
Milestone: | 5.9 | Priority: | normal |
Severity: | normal | Version: | 5.1 |
Component: | General | Keywords: | php73 has-patch has-unit-tests commit |
Focuses: | Cc: |
Description
PHP 7.3 will introduce two new functions, array_key_first()
, and array_key_last()
. The two functions return the first and last key of each array respectively, without affecting the internal state of the array (reset()
, end()
and key()
do). The full RFC can be read here: https://wiki.php.net/rfc/array_key_first_last
Including a polyfill would allow developers to take advantage of the new functions and write consistent code regardless of a site's PHP version. While polyfills for these two functions are not required for WordPress to be PHP 7.3 compatible, it could be added in the version after 7.3 compatibility is declared.
Attachments (3)
Change History (24)
#3
@
6 years ago
For array_key_first
, foreach
is faster than reset
while as for array_key_last
, end
is faster than other approaches like array_slice
or array_keys
with end
.
#4
@
6 years ago
- Keywords has-patch added; needs-patch removed
- Version set to trunk
I also think it's a good idea to add the polyfills, but also to search existing current()
, reset()
, and key()
calls to make use of this function. I know PHP 7.3 users will appreciate it too.
Patch from @manzoorwanijk is a good start, but now that PHP 7.3 is released, we can make it more closure to the native function. It looks like this patch was taken from the first example in this function page. Symfony/polyfills has a better one that mimics the original functionality with parameter types. I will attach a patch with more optimized polyfills. To make it complete, this patch also contains the original tests from PHP source, so we can make sure it really gives same results as PHP's own tests.
#6
@
3 years ago
- Milestone changed from Future Release to 5.9
- Owner set to desrosj
- Status changed from new to assigned
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
3 years ago
This ticket was mentioned in Slack in #core-php by hellofromtonya. View the logs.
3 years ago
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
3 years ago
#12
@
3 years ago
- Owner changed from desrosj to hellofromTonya
- Status changed from assigned to accepted
Reassigning this ticket and other PHP polyfill tickets to me to get them into 5.9 release.
This ticket was mentioned in PR #1809 on WordPress/wordpress-develop by pbearne.
3 years ago
#13
- Keywords has-unit-tests added; needs-refresh needs-unit-tests removed
added tests
Trac ticket: https://core.trac.wordpress.org/ticket/45055
This ticket was mentioned in Slack in #core-php by pbearne. View the logs.
3 years ago
hellofromtonya commented on PR #1809:
3 years ago
#15
Well darn. Working in the GH UI to edit the file is not capturing the tabs. Weird. Can be fixed during commit.
hellofromtonya commented on PR #1809:
3 years ago
#16
Well darn. Working in the GH UI to edit the file is not capturing the tabs. Weird. Can be fixed during commit.
hellofromtonya commented on PR #1809:
3 years ago
#19
Committed via changeset https://core.trac.wordpress.org/changeset/52038.
#20
@
3 years ago
Thank you everyone for your contributions! The polyfills and their tests are now committed and will ship in 5.9.
Related: #43583, #43619.