| alec | Yes | 2021-05-24 08:28 |
| asgrim | Yes | 2021-06-01 07:52 |
| ashnazg | Yes | 2021-05-20 14:55 |
| bmajdak | Yes | 2021-06-05 17:25 |
| brzuchal | Yes | 2021-05-19 08:12 |
| bwoebi | Yes | 2021-05-20 23:28 |
| crell | Yes | 2021-05-19 13:53 |
| derick | Yes | 2021-05-19 08:45 |
| galvao | Yes | 2021-05-23 19:27 |
| gasolwu | Yes | 2021-05-19 08:14 |
| geekcom | Yes | 2021-05-20 18:31 |
| girgias | No | 2021-05-23 23:39 |
| irker | Yes | 2021-05-28 19:20 |
| jbnahan | Yes | 2021-06-01 06:57 |
| jhdxr | No | 2021-05-19 19:23 |
| kalle | Yes | 2021-05-20 11:58 |
| kguest | Yes | 2021-05-28 09:05 |
| kocsismate | Yes | 2021-05-19 08:01 |
| lcobucci | Yes | 2021-05-23 19:02 |
| levim | No | 2021-05-24 14:23 |
| mbeccati | Yes | 2021-05-20 10:45 |
| mgocobachi | Yes | 2021-06-01 12:53 |
| nicolasgrekas | Yes | 2021-05-25 10:16 |
| ocramius | Yes | 2021-05-19 08:06 |
| reywob | No | 2021-05-24 07:25 |
| sebastian | Yes | 2021-05-19 08:05 |
| sergey | Yes | 2021-05-19 08:04 |
| svpernova09 | Yes | 2021-06-01 18:32 |
| tandre | Yes | 2021-05-20 22:22 |
| theodorejb | Yes | 2021-05-19 12:02 |
| twosee | Yes | 2021-06-02 07:45 |
| weierophinney | Yes | 2021-06-01 15:13 |
| wyrihaximus | Yes | 2021-05-20 12:39 |
Copied from wiki.php.net, last synchronized Oct 13, 2026 11:50 UTC
Currently, class constants are always overridable by child classes. This causes a few smaller caveats:
First of all, the engine can't optimize class constant references when late static binding is involved, so it has to pessimistically assume that FOO is overridden in case of static::FOO or $this::FOO invocations.
What's more important is that class constants are not guaranteed to stay constant. Even if the declaring class always references them by using self::, doing so won't prevent a child class from changing their value or even their type, unless the parent is a final class. Although constants not being constants is usually considered only as a theoretical problem, being able to add a final modifier would make the intention explicit that child classes shouldn't try to override them (e.g. because the parent doesn't use late static binding).
A related interesting fact is that interface constants are already final:
interface I
{
public const X = "i";
}
class C implements I
{
public const X = "bar";
}
// Fatal error: Cannot inherit previously-inherited or override constant X from interface I
This leads to a weird inconsistency. By introducing an intermediate class, overriding still becomes possible:
interface I
{
public const X = "i";
}
class C implements I
{
}
class D extends C
{
public const X = "d";
}
// No error
The final modifier can be added to class constants. Doing so prevents overriding of a constant:
class Foo
{
final public const X = "foo";
}
class Bar extends Foo
{
public const X = "bar";
}
// Fatal error: Bar::X cannot override final constant Foo::X
Besides, interface constants would become overridable by default, and the final modifier could be used to retain the original behavior.
interface I
{
public const X = "i";
final public const Y = "i";
}
class C implements I
{
public const X = "c"; // Overriding I::X is possible
public const Y = "c"; // Overriding I::Y is not possible
}
// Fatal error: C::Y cannot override final constant I::Y
A ReflectionClassConstant::isFinal() method is added in order to be able to retrieve if a constant is final.
None.
Voting started on 2021-05-19 08:00 UTC and ends 2021-06-02 08:00 UTC. The vote requires 2/3 majority to be accepted.
Vote: Add support for final class constants? — Yes: 29 · No: 4 (closed)