rfc:final_class_const
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:final_class_const [2021/04/23 08:35] – created kocsismate | rfc:final_class_const [2021/06/14 07:48] – nikic | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Date: 2021-04-23 | * Date: 2021-04-23 | ||
* Author: Máté Kocsis < | * Author: Máté Kocsis < | ||
- | * Status: | + | * Status: |
* Implementation: | * Implementation: | ||
+ | * Target Version: PHP 8.1 | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Currently, class constants are always overridable by any child classes. This causes a few smaller caveats: | + | 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 '' | First of all, the engine can't optimize class constant references when late static binding is involved, so it has to pessimistically assume that '' | ||
- | But more importantly, | + | What' |
- | let's consider the following code: | + | |
+ | A related interesting fact is that interface constants are already '' | ||
<code php> | <code php> | ||
- | class Foo | + | interface I |
{ | { | ||
- | public const X = "foo"; | + | public const X = "i"; |
- | + | ||
- | public function getX(): string | + | |
- | { | + | |
- | return self::X; | + | |
- | } | + | |
} | } | ||
- | class Bar extends Foo | + | class C implements I |
{ | { | ||
public const X = " | public const X = " | ||
- | | ||
- | public function getBar(): string | ||
- | { | ||
- | return self::X; | ||
- | } | ||
} | } | ||
+ | |||
+ | // Fatal error: Cannot inherit previously-inherited or override constant X from interface I | ||
</ | </ | ||
- | When overriding | + | This leads to a weird inconsistency. By introducing an intermediate |
+ | |||
+ | <code php> | ||
+ | interface I | ||
+ | { | ||
+ | public const X = " | ||
+ | } | ||
+ | |||
+ | class C implements I | ||
+ | { | ||
+ | } | ||
+ | |||
+ | class D extends C | ||
+ | { | ||
+ | public const X = " | ||
+ | } | ||
+ | |||
+ | // No error | ||
+ | </ | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | The final modifier can be added to class constants. Doing so prevents a constant | + | The final modifier can be added to class constants. Doing so prevents |
<code php> | <code php> | ||
Line 57: | Line 69: | ||
</ | </ | ||
- | Besides, interface constants would become overridable by default: | + | Besides, interface constants would become overridable by default, and the '' |
<code php> | <code php> | ||
Line 68: | Line 80: | ||
class C implements I | class C implements I | ||
{ | { | ||
- | public const X = " | + | public const X = " |
public const Y = " | public const Y = " | ||
} | } | ||
Line 77: | Line 89: | ||
===== Reflection ===== | ===== Reflection ===== | ||
- | A '' | + | A '' |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 85: | Line 97: | ||
===== Vote ===== | ===== Vote ===== | ||
- | Add support for final class constants? | + | 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. |
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
rfc/final_class_const.txt · Last modified: 2021/07/06 20:52 by kocsismate