Is there a way to specify class constant's type?
| From: | Jelly Legend | Date: | Mon, 29 Jun 2020 17:52:43 +0000 |
| Subject: | Is there a way to specify class constant's type? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-327216@lists.php.net to get a copy of this message | ||
Hi,
I tried using types on class constants which resulted in an error.
I'm trying to enforce a type of a constant on a child class, which can
currently be overwritten.
I want to use top abstract classes, a sort of the main data processors
and its children as configurations.
Example: https://pastebin.com/sWuFTrFr
I find it very clean to use constants for this purpose, but I haven't
found a way to enforce their type, other than using abstract static
methods and defining their types and implementations in children.
Example: https://pastebin.com/edit/bGURTbak
The behaviour I expected was that if a class constant's type is
defined then it's children could not override the type of the constant
i.e. redefine it but not change its type.
Example: https://pastebin.com/2LrcKjDp
Is there a way to achieve strict types on child constants other than
using methods?
Thank you!
Jelly