|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70986 Constants in traits
Submitted: 2015-11-27 14:37 UTC Modified: 2017-08-11 11:17 UTC
Avg. Score:4.4 ± 0.7
Reproduced:17 of 18 (94.4%)
Same Version:7 (41.2%)
Same OS:5 (29.4%)
From: owlet at avtovokzaly dot ru Assigned:
Status: Duplicate Package: Class/Object related
PHP Version: Irrelevant 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.
Block user comment
Status: Assign to:
Bug Type:
From: owlet at avtovokzaly dot ru
New email:
PHP Version: OS:


 [2015-11-27 14:37 UTC] owlet at avtovokzaly dot ru
Sometimes we need to define constants directly in trait. Now it is impossible and can be done throw namespace constants, for example, But this replacement very uncomfortable.

Test script:
trait A {
  const CONST_A = 'value of A::A';

class B {
  use A;

  public static function getA()
    return self::CONST_A;

echo B::getA();

Expected result:
value of A::A

Actual result:
PHP Fatal error:  Traits cannot have constants in php shell code on line 2


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-27 22:54 UTC]
The issue is that we don't have collision resolution for constants in case we use two different traits defining a constant with the same name…

In general, that's related to the general limitedness of traits being pure compiler copy&paste directly into a class and not just sharing scope…
 [2015-11-29 11:51 UTC] owlet at avtovokzaly dot ru
Is it possible to disable in this case classified the same name in different traits constants used in the same class? And leave the behavior the same as in a regular class with a redefined constant: Fatal error, with message "Cannot redefine class constant"?
 [2015-11-29 11:59 UTC] owlet at avtovokzaly dot ru
Or, perhaps, to use an expression for compiler in the same manner as for methods:

use A, B {
  B::CONST_A insteadof A;
 [2016-07-29 13:10 UTC]
For reference: <>.
 [2017-08-11 11:17 UTC]
-Status: Open +Status: Duplicate
 [2017-08-11 11:17 UTC]
Marking as duplicate of the more recent request #75060.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Dec 05 01:03:35 2021 UTC