Unexpected array indexing in class's static property
| Bug #66015 | Unexpected array indexing in class's static property | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Submitted: | 2013-11-01 00:53 UTC | Modified: | 2014-04-10 21:31 UTC |
|
||||||||||
| From: | jmcustiel at gmail dot com | Assigned: | bwoebi (profile) | |||||||||||
| Status: | Closed | Package: | Arrays related | |||||||||||
| PHP Version: | 5.4.21 | OS: | Win 7/Mac OS/Gnu-Linux(Ubuntu) | |||||||||||
| Private report: | No | CVE-ID: | None | |||||||||||
[2013-11-01 00:53 UTC] jmcustiel at gmail dot com
Description:
------------
I have a class with a static property of type array. This array starts with a key/value pair with a numeric key (value 1 defined in a constant) used to start the indexing. After that, just another two values, expecting the keys to be autogenerated after the initial index.
I expect the array keys to be correlative starting from 1, and to 3. But the array has only two keys starting from 0, and to 1.
I attached a test script, for the script I expect the output to be:
array (
1 => 'first',
2 => 'second',
3 => 'third',
)
but, instead I get:
array (
0 => 'second',
1 => 'third',
)
I tested it using [] and array() and get the same results.
It only happens when using self:: followed by constant name as first key in the static property, the next two code portions works as expected, both inside or outside of the class:
$arr = array(
1 => 'a',
'b',
'c'
);
$arr = array(
Test::FIRST => 'd',
'e',
'f'
);
Test script:
---------------
class Test
{
const FIRST = 1;
const SECOND = 2;
const THIRD = 3;
protected static $array = [
self::FIRST => 'first',
'second',
'third'
];
public function __construct()
{
var_export(self::$array);
}
}
$test = new Test();
Expected result:
----------------
array (
1 => 'first',
2 => 'second',
3 => 'third',
)
Actual result:
--------------
array (
0 => 'second',
1 => 'third',
)
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2013-11-27 12:43 UTC] jmcustiel at gmail dot com
-Operating System: Windows 7 +Operating System: Win 7/Mac OS/Gnu-Linux(Ubuntu)
[2013-11-27 12:43 UTC] jmcustiel at gmail dot com
[2013-11-27 12:55 UTC] jmcustiel at gmail dot com
-Package: *General Issues +Package: Arrays related
[2013-11-27 12:55 UTC] jmcustiel at gmail dot com
[2013-11-28 10:28 UTC] laruence@php.net
[2014-04-10 21:31 UTC] bwoebi@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: bwoebi
[2014-04-11 08:09 UTC] bwoebi@php.net
-Status: Assigned +Status: Closed