Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119766 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 40252 invoked from network); 29 Mar 2023 10:49:43 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Mar 2023 10:49:43 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5713E180507 for ; Wed, 29 Mar 2023 03:49:42 -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_H2,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-vs1-f49.google.com (mail-vs1-f49.google.com [209.85.217.49]) (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 03:49:41 -0700 (PDT) Received: by mail-vs1-f49.google.com with SMTP id h15so12981350vsh.0 for ; Wed, 29 Mar 2023 03:49:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680086981; x=1682678981; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=dyLs43hYOVE4nrPWr9rvvOLuz3MX4873OzvhWnJ9z1k=; b=IkQeFdfkS2qkumoHjIwJfscN8Rgi+iKEU0QBNZzBtOSO3133lbPPKB6+TxEs5YV6g1 9WNGIE1O4eOiVt9Fon2neUrBe+ydu+af2uWID10PSv6gwsz/lZx1Ur/TBboiL3spCU1H XdVFe1o4qFzSgrTDlMtOnjJKbLFGcZKxOBcZkPbGM3xVc5ueF11eWo2CclB9ifdIhf2I MQ9tfaVV5EhYxjK0eQm0lKOPXNbexD9VFIi2rwUMC9WyTGRGPb4jPDy/MUvpIggOFXLx 5Phr/SiGxM3IjzB6a1m6OQhAQVBZE6IKtGVdgfyyEPaDR93kGliEG2yDr+wS5+B4F5O7 YU5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680086981; x=1682678981; h=content-transfer-encoding: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=dyLs43hYOVE4nrPWr9rvvOLuz3MX4873OzvhWnJ9z1k=; b=qAr+BrCuj+BSdZIdMuuK3oaPVd2DVgKQ0qpdT4b02zGKAkdcRMSVCE8SP5vI3+pEKm V1WKtJ8duCadye/oySWj6PHVx7Hrx4+acZeghVozlZQrY2+fcJ2AZzaLd7124UDshCHy q7OTwsrvNdFhqngqwvFCwuM0e5NJv/n4r2aDrgXBj8FIlsQCsJsgvoRf3esAe3JtYlo4 wu3TFO26JnG3jaACNsMLi8HfGjnuBMSb4fN3mF6Vy4SN53PedKKsoRHWyY1t53ZJoAZY AALH4DSrTK1vf8h9wnLLlDHcYyebITLn67rh7ivq0Py1wsrVKXfc53JVek75tBjNl+wQ GbrA== X-Gm-Message-State: AAQBX9fdVU+jD4n2lCjmnurAvuVEVy7UlBBEk7cU6lGwdDSVU+DPnKZE r8VSiftzYVKB6D0ScNA/OsBGzfPY9RpVrt7qtdC5C0qpv4U= X-Google-Smtp-Source: AKy350ZjhBuAx/4ZtZEUAVm769ydeY3+HylYfIypO626HTlELGHWjMGq3S2deyEZ12LydhtGE+VcQYeb4mD1jxCCIeo= X-Received: by 2002:a67:d703:0:b0:426:3f9e:8408 with SMTP id p3-20020a67d703000000b004263f9e8408mr1211345vsj.3.1680086980869; Wed, 29 Mar 2023 03:49:40 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 29 Mar 2023 13:49:23 +0300 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [IDEA] allow extending enum From: raveren@gmail.com (=?UTF-8?Q?Rokas_=C5=A0leinius?=) 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 =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?