php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14064 Arrays defined as object properties treat defined constants differently.
Submitted: 2001-11-14 17:38 UTC Modified: 2001-11-15 02:20 UTC
From: andre at fscinternet dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 4.0.6 OS: i386-redhat-linux-gnu
Private report: No CVE-ID: None
 [2001-11-14 17:38 UTC] andre at fscinternet dot com
It appears that using defined constants to index an array within a class results in different behaviour than arrays defined without.

Within a class, the array treats the constant as a string literal, whereas in a regular array definition, the constant is properly evaluated.

Which of the two is the "correct" behaviour, and why do they differ between contexts?  Is there a way to get the results of Situation A in Situation B?

--(snip)--

<?php


define( 'TEST1', 1 );
define( 'TEST2', 2 );
define( 'TEST3', 3 );


// Situation A
$aTest = array(
           TEST1 => 'TEST1',
           TEST2 => 'TEST2',
           TEST3 => 'TEST3'
              );

print_r( $aTest );

#########################################################################

// Situation B
class Test
{
   var $aTest2 = array(
                  TEST1 => 'TEST1',
                  TEST2 => 'TEST2',
                  TEST3 => 'TEST3'
                      );

}

$oTest = new Test;

print_r( $oTest );

?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-15 02:20 UTC] derick@php.net
Seems to be fix in php-4.2.0dev, the (correct) output I get is:
Array
(
    [1] => TEST1
    [2] => TEST2
    [3] => TEST3
)
test Object
(
    [aTest2] => Array
        (
            [1] => TEST1
            [2] => TEST2
            [3] => TEST3
        )

)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 23:01:30 2024 UTC