php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44827 define('::') can be defined
Submitted: 2008-04-25 10:01 UTC Modified: 2009-12-30 19:15 UTC
Votes:4
Avg. Score:4.5 ± 0.9
Reproduced:3 of 4 (75.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: kalle@php.net Assigned: iliaa (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.*, 6CVS (2009-05-05) OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kalle@php.net
New email:
PHP Version: OS:

 

 [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()...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-25 10:11 UTC] kalle@php.net
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.
 [2008-10-25 21:18 UTC] cellog@php.net
now that namespace separator will be \, the fix is simple: grep for "::" and fail if a user tries to do it.
 [2009-06-08 01:27 UTC] iliaa@php.net
This bug has been fixed in CVS.

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/.
 
Thank you for the report, and for helping us make PHP better.


 [2009-06-08 13:10 UTC] tony2001@php.net
Reopened.
Patches for HEAD and 5_2 broke the build and have been reverted.
 [2009-06-29 15:25 UTC] vrana@php.net
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.
 [2009-12-30 19:15 UTC] svn@php.net
Automatic comment from SVN on behalf of iliaa
Revision: http://svn.php.net/viewvc/?view=revision&revision=292823
Log: Fixed bug #44827 (define() allows :: in constant names).
 [2009-12-30 19:15 UTC] iliaa@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/.
 
Thank you for the report, and for helping us make PHP better.


 [2010-01-25 13:08 UTC] svn@php.net
Automatic comment from SVN on behalf of iliaa
Revision: http://svn.php.net/viewvc/?view=revision&revision=293974
Log: Added test case for bug #44827
 [2010-01-25 23:41 UTC] svn@php.net
Automatic comment from SVN on behalf of johannes
Revision: http://svn.php.net/viewvc/?view=revision&revision=294034
Log: merge r292823 Fixed bug #44827 (define() allows :: in constant names). (iliaa)
and r293974 Added test case for bug #44827 (iliaa)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 12:01:27 2024 UTC