PHP 8.5 array_first() and array_last() break existing helper functions
Upgraded a legacy codebase from 8.3 to 8.5 this week. We have a helpers.php that defines array_first() and array_last() since around 2019. Now every request dies with “Fatal error: Cannot redeclare array_first()”. PHP 8.5 added these as native built-ins and there is no graceful fallback.
Had to grep the entire codebase, find every call site, and rename our helpers to something project-specific. Not a huge amount of work but completely unexpected for a minor version bump. Worth checking before you upgrade if you have a helpers file.
We hit the same thing but worse. We had the functions defined in our own helpers file, and two Composer packages also defined them conditionally with function_exists() guards. The guards save those packages but our unguarded definition still conflicts with the native one. Had to patch the one vendor file that did not use function_exists().
The function_exists() guard would have saved you: if (!function_exists(‘array_first’)) { function array_first(…) {} }. But now that it is a native function, the conditional version just never defines yours, which means any behavioral difference between your implementation and the native one becomes a silent bug.
Worth noting the behavioral difference too. The native array_first() returns null on an empty array and does not affect the internal array pointer. Some homegrown implementations returned false or threw an exception on empty. If you had === false checks downstream, those are now silent bugs after the rename.
@katedev good point. We had one place that checked the result against false specifically. Changed it to null check after finding the native docs. Fortunately it was covered by a test so it showed up before prod.
```php blocks are runnable.