Req #44568 [Opn->Bgs]: class name as defined constant
| From: | johannes@php.net | Date: | Wed, 22 Dec 2010 12:35:22 +0000 |
| Subject: | Req #44568 [Opn->Bgs]: class name as defined constant | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-157230@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=44568&edit=1
ID: 44568
Updated by: johannes@php.net
Reported by: andrea at 3site dot it
Summary: class name as defined constant
-Status: Open
+Status: Bogus
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: Windows XP
PHP Version: 5.2.6RC3
Block user comment: N
Private report: N
New Comment:
You can also have a function with the same name and a variable with the
name8prefixed by $), PHP has multiple name scopes.
Previous Comments:
------------------------------------------------------------------------
[2010-12-01 07:50:06] 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
------------------------------------------------------------------------
[2008-03-30 10:59:12] andrea at 3site dot it
Description:
------------
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.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=44568&edit=1