|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44568 class name as defined constant
Submitted: 2008-03-30 10:59 UTC Modified: 2010-12-22 13:35 UTC
Avg. Score:1.3 ± 0.7
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: andrea at 3site dot it Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.6RC3 OS: Windows XP
Private report: No CVE-ID: None
 [2008-03-30 10:59 UTC] andrea at 3site dot it
It seems to be an ambiguity issue and a probable future problem.
You can create a class and define a constant with the same name and vice-versa.
Who is the class and who is the constant?

For future problems: currently, the magic __toString method works as magic instance method even if it is declared as public static ( ... and inherited in instances as public without static ... but this is another problem ... ) If one day PHP would like to support a public static __toString method this ambiguity will be a problem to understand if we are trying to get them, or the possible defined constant.

Reproduce code:
class Test{}
define('Test', 123);

// defined('Test') or class_exists('Test') ?

define('Test2', 'Test2');
class Test2{}

get_class(new Test2) === Test2; // true
echo Test2; // Test2
$ref = Test2;
new $ref; // an instanceof Test2 ... 

is_string(Test2); // true

Expected result:
A fatal error, because a constant should be a unique and immutable value with a name that could not be used as class one and a class should be unique as well (if we cannot use the same name for two different classes, how can we have two totally different meaning with a sngle name, the class and the constant)

Actual result:
It is possible to create a class and then define a constant with the same name, and it is possible to define a constant and then a class with the same name.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-01 07:50 UTC] philip dot preisser at web dot de
Test2 Fatal error: Class 'Test2' not found in /var/www/HTTP/devel/current/frameworld/test.php on line 53
 [2010-12-22 13:35 UTC]
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-12-22 13:35 UTC]
You can also have a function with the same name and a variable with the name8prefixed by $), PHP has multiple name scopes.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Sep 25 17:05:53 2022 UTC