While waiting on a reply on https://github.com/elastic/elasticsearch-php/issues/1084, I'm stuck in a situation where I do have PHP 8.0 on my machine already, but cannot update my project because I cannot do a composer install on PHP 8.0. I can however run my already locally installed code in PHP 8.0, so there I got this bug.
I got the following Exception when running stuff with Elastic in PHP 8.0:
Elasticsearch\Connections\Connection::Elasticsearch\Connections\{closure}(): Argument #2 ($key) must be passed by reference, value given
at vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:357
353▕ $value = 'true';
354▕ } elseif ($value === false) {
355▕ $value = 'false';
356▕ }
➜ 357▕ }
358▕ );
359▕
360▕ $uri .= '?' . http_build_query($params);
361▕ }
The full method where this happens is here (comment mine):
private function getURI(string $uri, ?array $params): string
{
if (isset($params) === true && !empty($params)) {
array_walk(
$params,
## line 346:
function (&$value, &$key) {
if ($value === true) {
$value = 'true';
} elseif ($value === false) {
$value = 'false';
}
}
);
$uri .= '?' . http_build_query($params);
}
if ($this->path !== null) {
$uri = $this->path . $uri;
}
return $uri ?? '';
}
The solution is as simple as just removing the & before $key on line 346, or removing &$key all together (it's unused).
Note that the documentation on https://www.php.net/array_walk says:
Only the values of the array may potentially be changed; its structure cannot be altered, i.e., the programmer cannot add, unset or reorder elements. If the callback does not respect this requirement, the behavior of this function is undefined, and unpredictable.
... so I think that's why in PHP 8.0 this got stricter?
Hi,
I discovered this too in https://github.com/elastic/elasticsearch-php/pull/1063#issuecomment-727770331 and someone beat me making a PR for it (though unmerged as of now) => https://github.com/elastic/elasticsearch-php/pull/1075
@sebsel OT, but you can always do composer install --ignore-platform-reqs :)
@sebsel fixed in 7.x branch with #1075. I'll ship it in the next 7.11 release.
Most helpful comment
@sebsel fixed in
7.xbranch with #1075. I'll ship it in the next7.11release.