php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33905 Strange order of array elements if there are constants among array keys
Submitted: 2005-07-28 16:00 UTC Modified: 2005-07-28 16:25 UTC
From: s dot lystsev at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.4 OS: Fedora Core release 1
Private report: No CVE-ID: None
 [2005-07-28 16:00 UTC] s dot lystsev at gmail dot com
Description:
------------
If I initialize array immediately when declaring it as a class member (see "Reproduce code") and there are some constants among array keys, then the order of array elements is broken. All the elements which have a constant as a key - they are moved to the end of an array. Thus the declared order of elements is broken.

If I move initialization of array to some method (e.g. in class constructor) - everything works as expected, array is not reordered.

Well, the bug is close to Bug #28072, but there was a talk about static arrays. In my case the array was not static.

Reproduce code:
---------------
define ('baa', '_a_');
define ('baz', '_b_');
class T {
        var $test = array(
                baa     => 'xx',
                'bbb'   => 'x1',
                'aaa'   => 'x0',
                'ccc'   => 'x2',
                baz     => 'yy'
        );
        function process() {
                foreach ($this->test as $x => $y) {
                        echo "$x => $y\n";
                }
        }
}
$t = new T(); 
$t->process();


Expected result:
----------------
_a_ => xx
bbb => x1
aaa => x0
ccc => x2
_b_ => yy

Actual result:
--------------
bbb => x1
aaa => x0
ccc => x2
_a_ => xx
_b_ => yy

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-28 16:25 UTC] sniper@php.net
And you didn't bother trying the latest CVS snapshot?
(yes, it is fixed already)

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 27 13:00:02 2025 UTC