On migrating to PHP 7.4 I have to deal with a different behavior of some array functions like reset()
, current()
or end()
concerning ArrayObject. The following example produces different outputs:
<?php
$array = new \ArrayObject(["a", "b"]);
$item = end($array);
var_dump($item);
$array = ["a", "b"];
$item = end($array);
var_dump($item);
With php 7.4 the output is:
bool(false)
string(1) "b"
On PHP versions before 7.4 the output is the following:
string(1) "b"
string(1) "b"
A end($array->getArrayCopy())
produces a notice, but might be a workaround if used with a variable.
Is there a way to emulate the behavior of end()
with an ArrayObject
or ArrayIterator
? The ArrayObject could be very big, an iteration to the end might not be the best solution.