php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49431 Cannot define class constant with constant value
Submitted: 2009-09-01 16:27 UTC Modified: 2010-12-29 12:24 UTC
From: sweiss at stylesight dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.0 OS: Debian
Private report: No CVE-ID: None
 [2009-09-01 16:27 UTC] sweiss at stylesight dot com
Description:
------------
In the PHP documentation it states:

"It is possible to define constant values on a per-class basis 
remaining the same and unchangeable. Constants differ from normal 
variables in that you don't use the $ symbol to declare or use them.

"The value must be a constant expression, not (for example) a 
variable, a class member, result of a mathematical operation or a 
function call."

This is however not entirely the case.

If you define a constant within a class based on another constant, it 
will not work, despite the fact that it is a constant value.

In the given example, X_CONST . "Y" is a constant composed of two 
other constants - no variables, class members, math expressions, or 
function calls.

And, in fact, for normal, non-class constants, this is totally 
possible:

define("Y_CONST", X_CONST . "Y");

is totally valid.

At the very least the documentation should be updated to reflect 
simply that a class constant may only be defined as a simple scalar 
value, and not just any constant expression.

Reproduce code:
---------------
define("X_CONST", "Const");
 
class TestClass {
	
	const Y_CONST = X_CONST . "Y";

        function test() {
            return self::Y_CONST;
        }
}

$a = new TestClass();
var_dump($a->test());

Expected result:
----------------
(string) "ConstY"

Actual result:
--------------
Parse error: syntax error, unexpected '.', expecting ',' or ';' in line 
5

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-29 12:23 UTC] jani@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-12-29 12:23 UTC] jani@php.net
Same as bug #30177
 [2010-12-29 12:24 UTC] jani@php.net
-Package: *General Issues +Package: Scripting Engine problem
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 14:01:33 2025 UTC