Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124986 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 964FC1A00B7 for ; Fri, 16 Aug 2024 18:27:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1723832954; bh=xKN9EdW24C7fVDk7D7kTb818EvY2xPMRJ9/7fvphvkw=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=WRq7BFsZy0AcRBNr8V/ZikOaTARVRMmwg6fo9jbPKQKHko8fZqsTE5sKieNStOMfo oyQKzFB4I6C0zLbpt1VgZj7qDG83Mt+CGYvxQ/hZdinvzaJ9J2lvx79vL3Qw5PgbpJ hgmxddS4rapq4p/DyR43QusNEVRFtHpM7Bziv4ZZWwTLf3gdZi7VO3gceDuFUNnTFs a58APl1eeTbUrDUqyE2UwTZqVMeXj9OP0G6D3A6Ai4355g3ZlYwyrqMj9z7em3GywA S5wL4lJYR0TY7OAzZ5cG34nTTA1EBzfz9GDnnum97WYgI46/TDDP1tf6qwgELRpLW4 2TS/5BYGu7CeA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A18DA18007C for ; Fri, 16 Aug 2024 18:29:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 16 Aug 2024 18:29:13 +0000 (UTC) Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-371a6fcd863so116075f8f.1 for ; Fri, 16 Aug 2024 11:27:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723832844; x=1724437644; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Gookv1ppVv5estMTWPeJNU/n/KuRHcQz8e89M18VoVo=; b=ml4pUcOf08SnfJ0azlMmWX5vvL1zzhnaXbvyCZSxF3QodijWa6Ww95fWKBRzQyDuC5 DDzr81vvufBEUlQUNUbRuupEXjTOtOEmRcpqJP7KH+u55Ndg5aVwDah2Y/WyDabdDgbT PErxCVea/zZsYCojuNPtApga3+5UTUeU3dsXhlcVXhxIjw84RC9V6yx8vVz1+W65uIoT +h9ifI6bNVQuaFN0kmAYtPL83P4gMjAGtj5/yPClIgIBQCuWCstXB+GKPzCQdRPvhhZh YoemabGN+2z7hkZrlp98W0Kfn13sQe/lbBc6pV8DAK8S/Vee1Zdg049mOmam4xCwpbW5 LUHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723832844; x=1724437644; 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=Gookv1ppVv5estMTWPeJNU/n/KuRHcQz8e89M18VoVo=; b=hvz3fm8FEi99qEyitAsMZmbKazI0VET2a5XdEVsz5IOX4OjHz2/h/FlRzUIFSaupJY wuAxsfImoqQsMrIG8jwy22Dhlfz5Dr837PN5zv5fLYSgDKpljjODOFEGggrMFRfQj7sf 1dgdeHXu1Bye3EwsfphEoUGl4WvXMJLpfpeVY3BjX7EuXXBuAtZbTeanP1bP53PsE7KI wyuJ4P1vP73tFDO8TXqtDs5lCnCH8gkd++cSt4zsM5oCKh7CKVaNWeIdmkQc0hwHYVrh G7/HBMqaOyucrAf66S7rEoRQ2AEWb7K9lGqD3nTT5taHesf9+NNEs/moXpsV+8Rv3Kh1 SaXA== X-Gm-Message-State: AOJu0YwfVZGWiSR5wNAKis2BkBspWJS+EOD8ZF2rF0D6LufHcsyFwYTA 9N/4rkPNKgqUQiX2i1Gdis2aSGn1dYywHm9S3gA3Tg1FWVjro9w8IC9KQFYF0cMQiXaSjgPVe3c NOaAEsf39zxWiuqwDCHOIn0pjt5BfZK4v X-Google-Smtp-Source: AGHT+IEFisYhIUDnNFDe/Uz/0pQ2bCjbLpQsK1ADF1h7Y/h2YpZLKgH3AglnKzrBMDODc0LAmXrbA3+I+B/yXkO0PMU= X-Received: by 2002:a05:6000:1365:b0:366:ef03:a8a2 with SMTP id ffacd0b85a97d-371946c8dc5mr2933227f8f.54.1723832844160; Fri, 16 Aug 2024 11:27:24 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <34548342633f1d87b2fc77c643bd895a18f71b66.camel@ageofdream.com> In-Reply-To: <34548342633f1d87b2fc77c643bd895a18f71b66.camel@ageofdream.com> Date: Fri, 16 Aug 2024 21:26:48 +0300 Message-ID: Subject: Re: [PHP-DEV] [DISCUSSION] Class Constant Enums? To: Nick Lockheart Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000541d66061fd11c97" From: arvids.godjuks@gmail.com (Arvids Godjuks) --000000000000541d66061fd11c97 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, 16 Aug 2024 at 04:37, Nick Lockheart wrote: > Hi, > > Is there any interest in having enums as class constants? > > I'm often finding cases where I would like to have an enum inside of a > class, but don't want a free-floating enum that's basically like > another class. > > When dealing with state, it's nice to have a human readable const to > represent that state, but I always feel like they should be grouped > together. > > For example: > > class SSHClient { > > public const COMMAND_RESULT_SUCCESS =3D 0; > public const COMMAND_RESULT_FAILURE =3D 1; > public const COMMAND_RESULT_UNKNOWN =3D 2; > public const COMMAND_RESULT_TIMEOUT =3D 3; > > // ... > > } > > These constants would make sense as an enum, but they make no sense > outside of the SSHClient class that uses them. > > It seems that enums would be useful as class constants. There's a lot > of cases where a class implements a state machine and needs statuses, > but those status flags should be local to the class, not shared between > classes. > > Example: > > class SSHClient { > > public const enum CommandResult > { > case Success; > case Failure; > case Unknown; > case Timeout; > } > > // ... > } > > > // Usage: > > SSHClient::CommandResult::Success > As a heavy Enum users, I'd love this. Other said about going all the way as embedded classes, but I would voice the opinion that it's gonna feature creep like crazy and probably has a lot more work in it than anyone imagines at the moment - just look on how long it took work out aviz. Enums are very limited as classes go, so it would be probably far easier to implement them. If implementation is done in a way where it's easy to expand in the future, I don't see a need to delay for a bigger scope. That being said, I do have to ask about having methods and being able to implement an interface and add traits to the embedded enum? All of my enums implement this interface ``` $translator->trans('Email', locale: $locale= ), self::WHATSAPP =3D> $translator->trans('Whatsapp', domain: 'non_translatable', locale: $locale), self::BOTH =3D> $translator->trans('Both', locale: $locale)= , }; } } ``` Honestly, I would really like to do this right there in class embedded, as having a separate file really is overkill :) --=20 Arv=C4=ABds Godjuks +371 26 851 664 arvids.godjuks@gmail.com Telegram: @psihius https://t.me/psihius --000000000000541d66061fd11c97 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable



As a heavy Enum users, I'd lo= ve this.

Other said about going all the way as emb= edded classes, but I would voice the opinion that it's gonna feature cr= eep like crazy and probably has a lot more work in it than anyone imagines = at the moment - just look on how long it took work out aviz. Enums are very= limited as classes go, so it would be probably far easier to implement the= m. If implementation is done in a way where it's easy to expand in the = future, I don't see a need to delay for a bigger scope.

<= /div>
That being said, I do have to ask about having methods and being = able to implement an interface and add traits to the embedded enum?
All of my enums implement this interface
```
<?ph= p
declare(strict_types=3D1);
namespace App\Interface;

use Back= edEnum;
use Symfony\Contracts\Translation\TranslatableInterface;

= interface TranslatableEnumInterface extends TranslatableInterface, BackedEn= um {}
```=C2=A0

and subsequently loo= k like this:

```
<?php
declare(str= ict_types=3D1);
namespace App\Enum;

use App\Interface\Translatabl= eEnumInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
enum ChannelTypeEnum: int implements TranslatableEnumInterface
{=C2=A0 =C2=A0 case EMAIL =3D 1;
=C2=A0 =C2=A0 case WHATSAPP =3D 2;
= =C2=A0 =C2=A0 case BOTH =3D 3;

=C2=A0 =C2=A0 public function trans(T= ranslatorInterface $translator, string $locale =3D null): string
=C2=A0 = =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return match ($this) {
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self::EMAIL =C2=A0 =C2=A0=3D> $transl= ator->trans('Email', locale: $locale),
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 self::WHATSAPP =3D> $translator->trans('What= sapp', domain: 'non_translatable', locale: $locale),
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self::BOTH =C2=A0 =C2=A0 =3D> $transl= ator->trans('Both', locale: $locale),
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 };
=C2=A0 =C2=A0 }
}
```
Honestly, I w= ould really like to do this right there in class embedded, as having a sepa= rate file really is overkill :)

--
--000000000000541d66061fd11c97--