Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119759 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 23851 invoked from network); 29 Mar 2023 08:47:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Mar 2023 08:47:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BD55D180547 for ; Wed, 29 Mar 2023 01:47:55 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 29 Mar 2023 01:47:55 -0700 (PDT) Received: by mail-ot1-f44.google.com with SMTP id l39-20020a9d1b2a000000b006a121324abdso6253815otl.7 for ; Wed, 29 Mar 2023 01:47:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680079674; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=7bM7Q1IkfOuE9MoMpmdJRke4JkOklkmMis/47mmqxMc=; b=IJIzbZrLwwsj9BvPfv2kcU1kS/StN+PDHp09My/Wf7PBYn0/wurg90ClqjUOw+nSEV YVV3mIUIydTI5KfVNii2iAKrl/2fGa0ODoHZmA1VcMIXnYL1PmaPmgDPu9l4kpqVQ+JM zPT21iT+Tr9we3GthNdcw72jidEXuC6C9PuRYDuPkw+2AA+MbqTYr7tdW2zsdP+MWbuz oqZHmuYnc13RXz4fMpSv8WFZw7MwH6LMUvLk9VRzsVO0i51DdIOSuhiPcIEwA4ZrOJEN PSF2R1fr6r4iEi+5TPMMTYguU58KIgrtB5+sTLiSQkuNHJsDUBjf3kfcfGIr+Rvtzgpe zEDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680079674; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7bM7Q1IkfOuE9MoMpmdJRke4JkOklkmMis/47mmqxMc=; b=2niUdyXc/LwmmPRvz9uUvcyYdqh4Bnre6fkYhQkUcRzFk53nJR1T3kxYaAqHFZCK+l ymArcGqKYEqqdAbbhnwBf55ugLtFxlP6Fa/9oMxudqMLihMjS2SetqjWXGntQgjwkRpH 816aOtRWQj4EO0TsfS67TTxB0WY4TKasbpLA8+sD9oU6qs0OW+25V5xyaXEBOq5dNUQ+ ThVStNn4exQ1wbgi208A9qONOl65UWv2r3eUrWH9ULOcH3YTZBIgS0fD6e5M0p6mRUaZ 89SH9WcFu9TP+D7WtiEVw2GJiRu0KOWyxHvWP7fj4Jx5wEuDnF1TpK79PhQEFDSNBubH gP1w== X-Gm-Message-State: AO0yUKWBWJ7+3M+/7qa6yqJCk9oawlZQm6PgL+0kcws/9iGmK/vVxjqg EKR7kW1Izm2SLjBs3PgXAt0sfGraizj6c/RdAqo= X-Google-Smtp-Source: AK7set+sBGbVN6L+bwmozzsyhLXTqQjHQnFVdgIE3cCk+JcTNG7QZvnebsan45lhh/1o7K3+InHpTMK80psY+vbaQJw= X-Received: by 2002:a9d:744d:0:b0:699:7883:940d with SMTP id p13-20020a9d744d000000b006997883940dmr6157387otk.7.1680079674693; Wed, 29 Mar 2023 01:47:54 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 29 Mar 2023 10:47:43 +0200 Message-ID: To: =?UTF-8?Q?Rokas_=C5=A0leinius?= Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [IDEA] allow extending enum From: landers.robert@gmail.com (Robert Landers) On Wed, Mar 29, 2023 at 10:31=E2=80=AFAM Rokas =C5=A0leinius wrote: > > Enums were a very useful addition to PHP, however one aspect of them is n= either > 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 > Hey Rokas, My approach has been to use an intersection type like: BackedEnum&MyCustomBehaviorInterface This works for me but it'd be interesting if something like that wouldn't work for you.