Re: Class Constant Finalization
| From: | Nikita Nefedov | Date: | Wed, 25 Jan 2017 07:30:06 +0000 |
| Subject: | Re: Class Constant Finalization | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-97951@lists.php.net to get a copy of this message | ||
> On 25 Jan 2017, at 09:48, Scott Arciszewski <scott@paragonie.com> wrote:
>
> All,
>
> Given that we can now declare a class constant as public, protected, or
> private, can we also declare them final in 7.2?
>
> https://3v4l.org/rJG0V
>
> Ideally, this code would error on line 18 rather than 15.
>
> Scott Arciszewski
> Chief Development Officer
> Paragon Initiative Enterprises <https://paragonie.com>
In my book, ideally, there would be no class constant inheritance as inherited constants don't
really make sense when they are known at compile time. People often use this feature in order to get
things like this to work:
class LibLevelClass {
const FOO = "default";
public function doSomething() {
echo "the value of FOO is: " . static::FOO;
}
}
class ExtensionClass {
const FOO = "custom";
}
But this approach is error prone due to untyped nature of constants (one could override the constant
with an array value). In the end this inheritance is always unnecessary.