Skip to content

Add array_first() and array_last() in compat.php#9556

Closed
USERSATOSHI wants to merge 9 commits intoWordPress:trunkfrom
USERSATOSHI:add/array-first-last
Closed

Add array_first() and array_last() in compat.php#9556
USERSATOSHI wants to merge 9 commits intoWordPress:trunkfrom
USERSATOSHI:add/array-first-last

Conversation

@USERSATOSHI
Copy link
Copy Markdown

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


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Aug 21, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props tusharbharti, jorbin, peterwilsoncc, mukesh27, johnbillion.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Copy Markdown
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a few minor notes inline.

Copy link
Copy Markdown
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add missing ticket annotations

Comment on lines +10 to +12
/**
* Test that array_first() is always available (either from PHP or WP).
*/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* Test that array_first() is always available (either from PHP or WP).
*/
/**
* @ticket 63853
*
* Test that array_first() is always available (either from PHP or WP).
*/

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I didn't know, it has to be there too. thank you

Comment on lines +10 to +12
/**
* Test that array_last() is always available (either from PHP or WP).
*/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* Test that array_last() is always available (either from PHP or WP).
*/
/**
* @ticket 63853
*
* Test that array_last() is always available (either from PHP or WP).
*/

@USERSATOSHI
Copy link
Copy Markdown
Author

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.

@johnbillion
Copy link
Copy Markdown
Member

Here's the PHP implementation for reference: https://github.com/php/php-src/blob/0bf295944df7171acf47502d31dd7df8b9155d81/ext/standard/array.c#L4528-L4552

@peterwilsoncc
Copy link
Copy Markdown
Contributor

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.

@USERSATOSHI
Copy link
Copy Markdown
Author

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?

Copy link
Copy Markdown
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, thank you!

@github-actions
Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 60672
GitHub commit: fbc6af4

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Aug 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants