Thank you for all the insight, I feel like having extendable enums
is an agreeable and organic way forward, so if I may, I would like
to go ahead and create an RFC draft for this and keep adding the
discussion outcomes till it settles for good.

If I can get the Karma to do so, of course, my wiki name is
raveren, thank you!

Sidenote, just asking :) was this expected behaviour that I had to
analyse PHP source of the Docuwiki, create a new input field in
the registration form named "spam" and fill it to gain a wiki
username? Otherwise it would just keep expecting a different
answer from me no matter what email I put in...

R.

On Wed, 29 Mar 2023 at 11:31, Rokas Šleinius <rave...@gmail.com> wrote:
>
> Enums were a very useful addition to PHP, however one aspect of them is 
> neither
> explicitly documented - or seemingly even talked about.
>
> Enums were implemented as final so they cannot be extended nor can extend
> anything else.
>
> From a user perspective it's surprising - and actually limiting.
>
> USAGE EXAMPLE:
> I am making an error management system: each error presented to the user
> must have a unique code visible.
>
> ```php
> class SystemError
> {
>     public function __construct(
>         private string $errorText,
>         private ErrorCode $code
>     ) {
>     }
>
>     public function __toString():
>     {
>         return $this->errorText . ' ' . $this->code->toString();
>     }
> }
> // ...
>
> enum ErrorCode
> {
>     case Code_1;
>     case Code_2;
>
>     public function toString(): string
>     {
>         return 'Error code:' . substr($this->name, strlen('Code_'));
>     }
> }
> ```
>
> Now I want to modify it to support different modules with different
> namespaces for
> errors, e.g. an ApiError, simple enough:
>
> ```php
> enum BaseErrorCode
> {
>     // ...
> }
>
> enum ErrorCode extends BaseErrorCode
> {
>     case Code_1;
>     case Code_2;
>
>     // ...
> }
>
> enum ApiErrorCode extends BaseErrorCode {
>     // ...
>     function toString(): string
>     {
>         return 'Error code:API-' . substr($this->name, strlen('Code_'));
>     }
> }
> ```
>
> This results in a syntax error.
>
> PROPOSAL:
>
> Enums should be able to extend other enums.
>
> For a complete wishlist, add:
>  * abstract enums;
>  * enums allowed to implement interfaces;
>
> However since I have no experience in PHP source code, I can only
> provide the test suite for a possible PR this might have :(
>
> Do you think this is likely to get implemented?

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to