php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50816 Using class constants in array definition fails (works in PHP_5_2)
Submitted: 2010-01-21 22:14 UTC Modified: 2011-08-01 11:21 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:0 of 0 (0.0%)
From: spasic dot dejan at yahoo dot de Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3, 6 OS: *
Private report: No CVE-ID: None
 [2010-01-21 22:14 UTC] spasic dot dejan at yahoo dot de
Description:
------------
Some lines of code is worth a thousand words

Reproduce code:
---------------
class Foo {
  const ONE = 1;
  const TWO = 1;

  public static $mapWithConst = array(self::ONE => 'one', self::TWO => 'two',);

  public static $mapWithoutConst = array(17 => 'one', 17 => 'two',);
}

$mapWithConst = array(1 => 'one', 1 => 'two',);

$mapWithoutConst = array(Foo::ONE => 'one', Foo::TWO => 'two',);

var_dump(Foo::$mapWithConst);
var_dump(Foo::$mapWithoutConst);
var_dump($mapWithConst);
var_dump($mapWithoutConst);


Expected result:
----------------
array(1) {
  [1]=>
  string(3) "two"
}
array(1) {
  [17]=>
  string(3) "two"
}
array(1) {
  [1]=>
  string(3) "two"
}
array(1) {
  [1]=>
  string(3) "two"
}


Actual result:
--------------
array(1) {
  [1]=>
  string(3) "one"   // ???
}
array(1) {
  [17]=>
  string(3) "two"
}
array(1) {
  [1]=>
  string(3) "two"
}
array(1) {
  [1]=>
  string(3) "two"
}


Patches

bug50816.patch (last revision 2011-07-30 16:06 UTC by laruence@php.net)
50816-2.diff (last revision 2011-07-29 14:30 UTC by pierrick@php.net)
50816.diff (last revision 2011-07-29 00:54 UTC by pierrick@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-22 13:45 UTC] jani@php.net
Works as expected in PHP 5.2, fails with 5.3 and above.
 [2011-07-29 00:54 UTC] pierrick@php.net
The following patch has been added/updated:

Patch Name: 50816.diff
Revision:   1311900851
URL:        https://bugs.php.net/patch-display.php?bug=50816&patch=50816.diff&revision=1311900851
 [2011-07-29 04:14 UTC] pierrick@php.net
The following patch has been added/updated:

Patch Name: 50816-2.diff
Revision:   1311912867
URL:        https://bugs.php.net/patch-display.php?bug=50816&patch=50816-2.diff&revision=1311912867
 [2011-07-29 14:30 UTC] pierrick@php.net
The following patch has been added/updated:

Patch Name: 50816-2.diff
Revision:   1311949824
URL:        https://bugs.php.net/patch-display.php?bug=50816&patch=50816-2.diff&revision=1311949824
 [2011-07-29 14:32 UTC] pierrick@php.net
This bug is due to the fact that the function
zend_hash_update_current_key_ex try to determinate if an element of
the array is before/after an other one with the new key by looking at
their position in the Bucket. The problem is that the key of the
element we want to change is not the same as the other one which mean
they're not always in the same Bucket (almost never).

The only solution I found was to determine which element is first by
looping into the array itself element per element. I tried to optimise
the patch by first making sure the second element exists so that I
don't loop on the array for nothing.

Any other idea is welcome
 [2011-07-29 17:44 UTC] pierrick@php.net
-Assigned To: +Assigned To: dmitry
 [2011-07-30 15:24 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug50816.patch
Revision:   1312039483
URL:        https://bugs.php.net/patch-display.php?bug=50816&patch=bug50816.patch&revision=1312039483
 [2011-07-30 15:41 UTC] laruence@php.net
I use ht->pListHead to traverse the elements in array , to find out who come 
first.

that should make the code more clearly.

patch submitted.
 [2011-07-30 16:06 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug50816.patch
Revision:   1312041987
URL:        https://bugs.php.net/patch-display.php?bug=50816&patch=bug50816.patch&revision=1312041987
 [2011-07-30 16:37 UTC] laruence@php.net
after have a conversation with pierrick , we decided that the patch "50816-2.diff 
" is the patch to apply if it's ok.

sorry for multi-submitted patches.
 [2011-08-01 11:21 UTC] dmitry@php.net
Automatic comment from SVN on behalf of dmitry
Revision: http://svn.php.net/viewvc/?view=revision&revision=314013
Log: Fixed bug #50816 (Using class constants in array definition fails).
 [2011-08-01 11:21 UTC] dmitry@php.net
-Status: Verified +Status: Closed
 [2011-08-01 11:21 UTC] dmitry@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2012-04-18 09:49 UTC] laruence@php.net
Automatic comment on behalf of dmitry
Revision: http://git.php.net/?p=php-src.git;a=commit;h=8ff0701e8d40171aae71d4650f515ba83e574566
Log: Fixed bug #50816 (Using class constants in array definition fails).
 [2012-07-24 23:40 UTC] rasmus@php.net
Automatic comment on behalf of dmitry
Revision: http://git.php.net/?p=php-src.git;a=commit;h=8ff0701e8d40171aae71d4650f515ba83e574566
Log: Fixed bug #50816 (Using class constants in array definition fails).
 [2013-11-17 09:37 UTC] laruence@php.net
Automatic comment on behalf of dmitry
Revision: http://git.php.net/?p=php-src.git;a=commit;h=8ff0701e8d40171aae71d4650f515ba83e574566
Log: Fixed bug #50816 (Using class constants in array definition fails).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 05:01:29 2024 UTC