Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119758 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 22853 invoked from network); 29 Mar 2023 08:47:36 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Mar 2023 08:47:36 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 91C131804F7 for ; Wed, 29 Mar 2023 01:47:34 -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,HTML_MESSAGE, 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-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) (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:31 -0700 (PDT) Received: by mail-ed1-f50.google.com with SMTP id b20so60263865edd.1 for ; Wed, 29 Mar 2023 01:47:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680079650; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=1OZwTukR4cWoeciZkZrAAwktx4RN6/ogevAYTyaRHZI=; b=PMSH2CMoDCWYC+vjkFbwicenfuhB+ss+X0nOHPyctWoq/vsd+tFGFLzfPbt71kmJvw qRvbOJtS12w4+KGK+PEoROWx1+1INjtYDXkh6EOC1YsXFFaYe72FeQSz/YzU0A1YzdIE AE8KZNa/cg8vhyGt8cSNyodf686vxQwxWXwGGj5V3pYFhy3+HK4Mp812DDkdlCfuQbax HwyIUGZ0VrI053KTDq76cd0x3IGkdKv27/UmHGIvdoReMBZYnoUV2rkA9m1mDDK9eSVx AjxheSLKBgtCVEx5zqRREKM6t9zuHMc3NEqvnTVJ3WDBxF8aXu5UiZlU8hX17fuAhFR3 Yt3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680079650; h=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=1OZwTukR4cWoeciZkZrAAwktx4RN6/ogevAYTyaRHZI=; b=ZjPVQt4VLYvm9JpfmfMHRE9OxuTyYJN6KAmG3vDQkmwEkBAsmpPZEvlX8AeD54P/OS oOAXorIWN5GUkQEm9Xq5goNnpr8MualTUtY9uTinCl0ilDCx941GIf6O6znjZxgVF313 C++dmfNGTNAdRxeCkc1dg+kqzuZAaKmUWRBTw4t1wCJzj5lvt35MfKTXsx8/wiMP9Pv7 mLdleWuErfjsaEp7uFeAlznbk4lII+kGkZrhsSIiWkVsukpOQ8UhVWdhDsEH57gE5QuB FJ+9JaLqOkOXzmYA9K2lzt34ezpjdyAPsJtCy3PBxKnmdVTo37zSU/piSeT+DeG5Zw7S a1PQ== X-Gm-Message-State: AAQBX9doVOadP0Fd74s5+Ndssde+qrT537XVT1KZwarlSoluM5a8NbQC 4siRY/GvB2npTpiVhMzX81bjTiUk/V1MGSVtSsFVBPQxR3RQ5A== X-Google-Smtp-Source: AKy350bcmT8PmMjzILkjp0WXRl4pPFOrWOwtvIsdgKnqZ+IJArhwXXsuwkCtD07ma2hG+FNqpOjrjOvcCBKK5rXzypc= X-Received: by 2002:a17:906:8683:b0:939:a51a:dc30 with SMTP id g3-20020a170906868300b00939a51adc30mr779111ejx.2.1680079649799; Wed, 29 Mar 2023 01:47:29 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 29 Mar 2023 10:47:18 +0200 Message-ID: To: =?UTF-8?Q?Rokas_=C5=A0leinius?= Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000b882a505f8060684" Subject: Re: [PHP-DEV] [IDEA] allow extending enum From: ocramius@gmail.com (Marco Pivetta) --000000000000b882a505f8060684 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable The point of an ENUM is precisely to restrict options within a certain range. Assume you declared `ErrorCode` with `SOMETHING_BROKE` and `PEBKAC` as possible enumeration values. If you can (somewhere, anywhere in the code) check `instanceof ErrorCode`, then you know it can only be `SOMETHING_BROKE` and `PEBKAC`, and you can write logic around it accordingly. This restriction/assumption is what makes ENUMs powerful: expanding pre-existing enums would eliminate this very core design principle, since you may now no longer have an `ErrorCode`, but some child class designed somewhere that may not even be under your control. Greets, Marco Pivetta https://mastodon.social/@ocramius https://ocramius.github.io/ On Wed, 29 Mar 2023 at 10:31, Rokas =C5=A0leinius 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 > > --000000000000b882a505f8060684--