php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66015 Unexpected array indexing in class's static property
Submitted: 2013-11-01 00:53 UTC Modified: 2014-04-10 21:31 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: jmcustiel at gmail dot com Assigned: bwoebi
Status: Closed Package: Arrays related
PHP Version: 5.4.21 OS: Win 7/Mac OS/Gnu-Linux(Ubuntu)
Private report: No CVE-ID:
Password:
Status:
Package:
Bug Type:
Summary:
From: jmcustiel at gmail dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [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
Reproduced the bug in MacOS and Ubuntu.
 [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
Changed the package field's value.
 [2013-11-28 10:28 UTC] laruence@php.net
actually, this is "kind of" not bug...

self::first will be expanded in runtime. 
before that, the array is array(0 => 'second', 1 => 'third' ,'self::FIRST' => 'fist')

in runtime, when doing class static member constants expanding, then self::FIRST expanded to 0, which will be failed since the 0 key is occupied..
 [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
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f614fc68984b2d7fce3f275b8106955b5d910472
Log: Fix bug #66015 by reverting "Removed operations on constant arrays."
 [2014-04-11 08:09 UTC] bwoebi@php.net
-Status: Assigned +Status: Closed
 [2014-04-11 08:12 UTC] bwoebi@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f614fc68984b2d7fce3f275b8106955b5d910472
Log: Fix bug #66015 by reverting "Removed operations on constant arrays."
 [2014-04-15 12:04 UTC] ab@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f614fc68984b2d7fce3f275b8106955b5d910472
Log: Fix bug #66015 by reverting "Removed operations on constant arrays."
 [2014-04-15 13:05 UTC] ab@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f614fc68984b2d7fce3f275b8106955b5d910472
Log: Fix bug #66015 by reverting "Removed operations on constant arrays."
 [2014-05-01 14:59 UTC] tyrael@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f614fc68984b2d7fce3f275b8106955b5d910472
Log: Fix bug #66015 by reverting "Removed operations on constant arrays."
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC