|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-04-25 10:01 UTC] kalle@php.net
Description:
------------
PHP (5.2.5) allows you to define '::' as a constant without issuing an E_WARNING, below examples will demonstrate it.
The constant() function will issue an fatal error when recieving '::' as constant name:
Fatal error: Class '' not found in C:\webserver\www\gd\gd.php on line 91
Which might also be wrong or confusing to the programmer
Reproduce code:
---------------
<?php
define('::', true);
var_dump(constant('::'));
?>
Expected result:
----------------
A fatal error for each of the calls:
Fatal error: Invalid constant name in test.php on line 2
Fatal error: Invalid constant name in test.php on line 3
Actual result:
--------------
Fatal error: Class '' not found in C:\webserver\www\gd\gd.php on line 91
None warning or fatal error for define()...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 23:00:01 2025 UTC |
As a note to this while we're at define(). define() also seems to ignore if you declare a class constant like: define('test::c', 'test'); Both with and without having a class called 'test' passes this, testing without the class 'test' like: var_dump(test::c); Will result in a fatal error: class 'test' not found. Testing it with a class called 'test' will issue a fatal error: undefined class constant 'c'. If the class 'test' already have a constant called 'c' define() will still ignore it and var_dump() will return the real value of test::c.Code in ZendEngine2/zend_builtin_functions.c revision 1.277.2.12.2.25.2.52 checks if the class exists and creates the global constant with the name A::B if it does. But this constant is unreachable other than by get_defined_constants() because both constant("A::B") and A::B works with class constants. <?php class A {} define("A::B", true); echo constant("A::B"); // Fatal error: Undefined class constant 'B' echo A::B; // Fatal error: Undefined class constant 'B' ?> I suggest to issue an error if the constant name contains "::" regardless the class exists or not. The code would be even simpler.