php.net |  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
Votes:18
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: owlet at avtovokzaly dot ru
New email:
PHP Version: OS:

 

 [2015-11-27 14:37 UTC] owlet at avtovokzaly dot ru
Description:
------------
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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-27 22:54 UTC] bwoebi@php.net
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] cmb@php.net
For reference: <https://3v4l.org/e3SMZ>.
 [2017-08-11 11:17 UTC] cmb@php.net
-Status: Open +Status: Duplicate
 [2017-08-11 11:17 UTC] cmb@php.net
Marking as duplicate of the more recent request #75060.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Dec 03 05:03:35 2021 UTC