php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Thank you for your help! If the status of the bug report you submitted changes, you will be notified. You may return here and check the status or update your report at any time.
The URL for your bug report is: https://bugs.php.net/bug.php?id=48858.
Bug #48858 array item definition problem (regression!)
Submitted: 2009-07-08 22:15 UTC Modified: 2013-09-16 17:24 UTC
Votes:8
Avg. Score:4.2 ± 0.8
Reproduced:8 of 8 (100.0%)
Same Version:8 (100.0%)
Same OS:4 (50.0%)
From: justin dot carlson at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.0 OS: *
Private report: No CVE-ID: None
 [2009-07-08 22:15 UTC] justin dot carlson at gmail dot com
Description:
------------
Array items are not as defined when using the same key twice.

I have not yet tested this outside of a class.







Reproduce code:
---------------
<?php

Foo::Bar();

abstract class Foo {

        const A = 1;
        const B = 2;
        const C = 3;
        const D = 1;
     
        protected static $sample = array(
                self::A => 'Apple',
                self::B => 'Boy',
                self::C => 'Cat',
                self::D => 'Dog'
                );
       
    public static function Bar() {
        print_R( self::$sample );
    }
}       
?>

Expected result:
----------------
In PHP 5.2.x:

Array
(
    [2] => Boy
    [3] => Cat
    [1] => Dog
)

This is what I would expect to be valid, as the 2nd entry would override the first, making "Dog" the correct value for index 1.

Actual result:
--------------
In PHP 5.3.x:

Array
(
    [1] => Apple
    [2] => Boy
    [3] => Cat
)

This was not expected, as the 1st entry maintained it's value.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-09 07:25 UTC] sjoerd-php at linuxonly dot nl
Thank you for your bug report. 

I was able to reproduce the problem and make the testcase somewhat simpler:

<?php
class Foo {
        const A = 1;
        const D = 1;
     
        public static $sample = array(
                self::A => 'First',
                self::D => 'Last'
        );
}       
print_r(Foo::$sample);
?>
 [2009-07-09 14:37 UTC] justin dot carlson at gmail dot com
I've confirmed this in Linux as well.

The following works fine:

<?php
    define('a',1);
    define('b',1);
    $test = array(a => 'foo', b => 'bar');
    print_R($test);
?>

So, it may be related to class constants?
 [2009-12-08 08:36 UTC] mp at webfactory dot de
The problem only occurs when the array is initialized in the variable declaration statement in the class AND class constants (with identical values) are used as keys.
 [2013-09-16 16:10 UTC] cmbecker69 at gmx dot de
According to <http://3v4l.org/ahcBe> this behavior has already 
been fixed with PHP 5.3.7.
 [2013-09-16 17:24 UTC] nikic@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Mon Jul 13 08:00:01 2026 UTC