Make array_find available for PHP < 8.4

array_find is a useful function, but only available in PHP 8.4 or later.
Here we implement it ourselves if it's not already defined, to make it
available for all supported PHP versions.
This commit is contained in:
Harald Eilertsen
2026-05-16 09:32:26 +02:00
parent 674522c8da
commit 6f65d2d6ef
2 changed files with 29 additions and 0 deletions

View File

@@ -42,6 +42,7 @@ use Zotlabs\Web\HttpMeta;
require_once('vendor/autoload.php');
require_once('include/php_compat.php');
require_once('include/config.php');
require_once('include/network.php');
require_once('include/plugin.php');

28
include/php_compat.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
/*
* Forward compatibility with more recent PHP versions.
*
* SPDX-FileCopyrightText: 2026 The Hubzilla Community
* SPDX-FileContributor: Harald Eilertsen <haraldei@anduin.net>
*
* SPDX-License-Identifier: MIT
*
* This file contains functions that allow us to use convenient functions from
* later PHP versions in earlier versions where these functions may not be
* supported directly.
*/
if (!function_exists('array_find')) {
// array_find is defined in PHP 8.4 or higher, so for earlier PHP versions we
// define it here.
function array_find(array $array, callable $callback): mixed {
foreach ($array as $key => $entry) {
if ($callback($entry, $key) === true) {
return $entry;
}
}
return null;
}
}