Bug #71529 [Com]: Variable references on array elements don't work when using count
Edit report at https://bugs.php.net/bug.php?id=71529&edit=1
ID: 71529
Comment by: inefedor at gmail dot com
Reported by: David dot Gausmann at measX dot com
Summary: Variable references on array elements don't work
when using count
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: irrelevant
PHP Version: 7.0.3
Block user comment: N
Private report: N
New Comment:
Here's a smaller reproducing script https://3v4l.org/q2mmT
PHP 7 has some changes in the way how references work, they may influence such non obvious changes
in behavior. I would recommend you using objects instead of arrays with references, that way this
code would also be easier to read and test.
Previous Comments:
------------------------------------------------------------------------
[2016-02-05 08:55:36] David dot Gausmann at measX dot com
Description:
------------
In a websites code we have got nested arrays for a tree structure.
In PHP 5.5 that code worked fine, but since PHP 7 it doesn't work anymore.
I've read the "backward incompatibility changes", but this point seems not to be
listed there (and in my opinion it is a bug).
In my code I am pushing variables by-reference into an stack array. The references are referencing
to the last element of another entry of the stack array.
The bug seems to depend on the usage of count(). If I use fixed indices for testing purposes, then
the code works fine.
Test script:
---------------
<?php
$avOutput = array('test' => array());
$avStack = array(&$avOutput['test']);
$avTemp = array('data' => 1, 'test' => array());
$avStack[count($avStack) - 1][] = $avTemp;
$avStack[] = &$avStack[count($avStack) - 1][count($avStack[count($avStack) - 1]) -
1]['test']; // WORKS NOT in PHP7 (but worked in PHP 5.5)
//$avStack[] = &$avStack[0][0]['test']; // WORKS
$avTemp = array('data' => 2, 'test' => array());
$avStack[count($avStack) - 1][] = $avTemp;
echo "<xmp>";
print_r($avOutput);
echo "</xmp>";
?>
Expected result:
----------------
Array ( [test] => Array ( [0] => Array ( [data] => 1 [test] => Array ( [0] => Array (
[data] => 2 [test] => Array ( ) ) ) ) ) )
(You can see that [data] => 1 and [data] => 2 are both listed. This was the result in PHP 5.5)
Actual result:
--------------
Array ( [test] => Array ( [0] => Array ( [data] => 1 [test] => Array ( ) ) ) )
(You can see that [data] => 1 is there, but [data] => 2 is missing. If you uncomment the line
with fixed indices, you become the correct result. If you output the result of the both count(), you
will get 0 for each, so both lines should be equal.)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71529&edit=1
Thread (4 messages)