Req #75060 [NEW]: Allow traits to have constants to support emerging use cases
| From: | jocrutrisi at ibsats dot com | Date: | Thu, 10 Aug 2017 18:43:07 +0000 |
| Subject: | Req #75060 [NEW]: Allow traits to have constants to support emerging use cases | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-210588@lists.php.net to get a copy of this message | ||
From: jocrutrisi at ibsats dot com
Operating system: All
PHP version: 7.2.0beta2
Package: Class/Object related
Bug Type: Feature/Change Request
Bug description:Allow traits to have constants to support emerging use cases
Description:
------------
I want to preface this with a disclaimer that I'm aware that "traits" in
other languages are even more conservative than PHP is - they don't
allow properties, while PHP does.
But what we have in PHP is still a useful variation of traits, called
"mixins". A mixin is a slice of a class that has properties, methods,
constants and so it can contain everything needed for implementing a
standalone "slice" of functionality for reuse in classes.
But while PHP allows properties, it doesn't allow constants in traits.
The reasons for this decision are unclear.
After traits were introduced in PHP 5.4, we've seen the roles, purpose
and use scenarios for constants increasingly grow over time:
1. PHP 5.6 allowed for array constants, which made constants handy for
constant look-up maps etc.
2. PHP 7.0 introduced opcache as a built-in feature, and it places
constants in shared memory, which makes them especially performant for
big look-up maps.
3. PHP 7.1 introduced private/protected constants, which means constants
can now be used as an internal implementation detail, and not
necessarily a public constant that can be specified in a separate class
or interface.
Unfortunately traits still don't allow constants... it's easy to see use
cases for them, here's a very basic example:
trait ColorTrait {
protected $color;
protected const COLORS = [
'red' => true,
'green' => true,
'blue' => true
];
function setColor($color) {
if (!isset(self::COLORS[$color])) {
throw new Exception('Meh');
}
$this->color = $color;
}
}
Note, I'm aware of a duplicate of this bug: #70986, but since it's from
2 years ago I wanted to make a better case for the feature, in light of
PHP 7.1's improvement on constants.
The problem raised in the original bug report is: what happens if
different constants of the same name are in different traits?
I propose an easy to implement conservative solution:
Fatal Error "Cannot use traits with the same constant names".
We can always refine the solution now, and reduce the surface of
rejected trait combinations later on, but to at least support the basic
use cases, which now we're also denied.
Test script:
---------------
trait Foo {
const BAR = 123;
}
Expected result:
----------------
No errors.
Actual result:
--------------
Fatal error: Traits cannot have constants
--
Edit bug report at https://bugs.php.net/bug.php?id=75060&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75060&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75060&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75060&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=75060&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=75060&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=75060&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=75060&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=75060&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=75060&r=support
Expected behavior: https://bugs.php.net/fix.php?id=75060&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=75060&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=75060&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=75060&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75060&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=75060&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=75060&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=75060&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75060&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=75060&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=75060&r=mysqlcfg