Req #70986 [Opn->Dup]: Constants in traits
| From: | cmb@php.net | Date: | Fri, 11 Aug 2017 11:17:17 +0000 |
| Subject: | Req #70986 [Opn->Dup]: Constants in traits | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-210603@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70986&edit=1
ID: 70986
Updated by: cmb@php.net
Reported by: owlet at avtovokzaly dot ru
Summary: Constants in traits
-Status: Open
+Status: Duplicate
Type: Feature/Change Request
Package: Class/Object related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Marking as duplicate of the more recent request #75060.
Previous Comments:
------------------------------------------------------------------------
[2016-07-29 13:10:21] cmb@php.net
For reference: <https://3v4l.org/e3SMZ>.
------------------------------------------------------------------------
[2015-11-29 11:59:08] 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;
}
------------------------------------------------------------------------
[2015-11-29 11:51:54] 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-27 22:54:29] 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-27 14:37:35] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70986&edit=1