5

I have this piece of PHP code:

echo 'PHP ' . phpversion()."\n";

$arr = array();
for ($i=0; $i<10; $i++)
{
    $arr[] = new stdClass();
}

$it = new ArrayIterator($arr);

$i = 1;
foreach ($it as $obj)
{
    $obj->test = $i;
    $i++;
}

var_dump(current($it));

die();

Output on PHP 5.3 is as follows:

PHP 5.3.8
object(stdClass)#805 (1) {
  ["test"]=>
  int(1)
}

Whereas output on PHP 7.4.8 is as follows:

PHP 7.4.8
file.php:33:boolean false

Why isn't this working anymore?

andreas
  • 149
  • 3
  • 12
  • Looks like it changed in 7.4.0, maybe a bug. https://3v4l.org/JfnT3 – AbraCadaver Jul 23 '20 at 15:59
  • Ah, it seems to be working in 7.3. okay, then it may be a bug – andreas Jul 23 '20 at 16:02
  • 1
    Using `current()` and [the other array functions](https://www.php.net/manual/en/function.current.php#refsect1-function.current-seealso) that use the internal pointer of an array produce code that is difficult to understand. – axiac Jul 23 '20 at 18:43
  • @axiac would you be against using `end()` as well, to get the last entry of a (possibly associated) array? – noam Jul 23 '20 at 19:26
  • Using `end()` only for its return value is fine. The same result can also be obtained by other means but `end()` is more convenient. The same for its counterpart, `reset()`. – axiac Jul 23 '20 at 19:38

0 Answers0