Re: Is there a way to specify class constant's type?
| From: | Christoph M. Becker | Date: | Tue, 30 Jun 2020 14:15:11 +0000 |
| Subject: | Re: Is there a way to specify class constant's type? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-327217@lists.php.net to get a copy of this message | ||
On 29.06.2020 at 19:52, Jelly Legend wrote:
> 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?
No, but typed constants have been recently proposed:
<https://externals.io/message/110755>.
--
Christoph M. Becker