Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123409 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 C7FA51A009C for ; Thu, 23 May 2024 06:35:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1716446181; bh=HZBcmEJ8aOwhhB316fVX/EIQFrOOChMh3ocT5MB4Yuw=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=f+0oGuIbKHkepjd0Ehr1xq7Uj2mIgGYUtNoZWMjU7mWAi3F+F+Zs1sAGu6y9MBi7x Cjn8rTtZgWXtg7N6luK8bxWdBZ/tBuJRbc0mtdiaQX29jyQUWFyfNhhZFlEXseEMFi s0ztxVflXnI6ODHJTibPtJcUlYcChRKNe0VrF4bXUsPlHxDw1AB7XJbTiM52LiJUCy FmJkVryAfUVFatLVHdtvtzzmQP/i3hn99N3gzmz62EcehGFEGljtIULWzrYnbaZAbM mGRNKvjVXeUiyS3FZLVheF22s5tEWDvng6FeuKOWtQzmSgDRtjJlEhAgxwVwjXCQfM 5v69XfEywQ6ZA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A5886180061 for ; Thu, 23 May 2024 06:36:20 +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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (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 ; Thu, 23 May 2024 06:36:20 +0000 (UTC) Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-51f99f9e0faso8800057e87.2 for ; Wed, 22 May 2024 23:35:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716446122; x=1717050922; darn=lists.php.net; 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=sUCzmFwYHy4lzLrMgWfKJy238IOXm5Y1ks3y7isE6Bs=; b=HOTh7QgnN7W8hAyuNNE+tGxO3jw6krF06Vgmv3BF4MX5DJtntiBlsXNyzVzMYkbnNR +Qq/An/D2CawCQpvn7P4/qjYbBCzpMZ/xUVuwalK0V3FdnD3mKxKHWVH1Awb6zeql5/Q 3tRWJZHSdDrr4ZWgAcTkwikh05JYppeAIKxuRrIFern79yfAHd2VFliOFkYPPB7tLZMd 3AwTu3WVBJcr/Y8Dw5DZlYnvMZWci3oEU6Dupr7CPdVRo+b+rFfO56tQOZDTz83GdSJw tbQQ4rbFgE+8Lq+G9HtRIr54LnOCvfgmmo5Vy3y7A5QzqadirfHDK6R93x/sGfXRwBk0 dy9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716446122; x=1717050922; 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=sUCzmFwYHy4lzLrMgWfKJy238IOXm5Y1ks3y7isE6Bs=; b=NcwgvKFBsrADqIBJb/LoGZ3/laSXKpH8bc2WKsolveF0FvIRNV5JZP3rMyfnZDY+XE ljnrsJuB2qhosxTgFfHuoSOp/dAFveT2yoeWeIjfUCPM3v9K6qOONMx2gGwgb7QKcHO2 Qr1XAoDyw5xm45n5MiMjRnjwmmQ4cI7aYutRa4cXtUbRaQCTxOan1xjxAVvXSDbnALgy ETxPZge/Z78S+qjrak7QB3BVHBpgFaq8VRON5xfTgrpS+fYs0kTK9ay/D+b9rNPw7uaO Q0rU9xchGT9w1f4XS7jXvOJGQkRV3WpiFCHIcGtjzdCofCIeAOI3QWpRWLsgyrABpzpa O01w== X-Gm-Message-State: AOJu0YzlsnBP6Z0TK4h+2ThNlw9W8pY6XkEujEjYcd7ZQBGxuBlXOBAS MKxGzqCP32XE15sOVU1WKDBG0jyvutMkr+xej4SytzbAfemF7MO370GzE04yY4Gii1zcob72GBD tmPTn5/uAXg9sPIKlvbokpkcP2oQGokPL X-Google-Smtp-Source: AGHT+IFWA/inTe9lqonaBRrFqX/oxg5sRtsFzbb3kOb2nrkACxYrOovmd5kOaXz/vv4TPYdn1aCihO8M2gk4iHEDf/4= X-Received: by 2002:a19:c218:0:b0:51d:2017:e65 with SMTP id 2adb3069b0e04-526bf54619fmr2450028e87.66.1716446121669; Wed, 22 May 2024 23:35:21 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <99c30f5d-e2f3-4027-a8fc-b3b9c71fb6d1@scriptfusion.com> <1286eed1-c35e-40ec-a6f0-84926c99c911@app.fastmail.com> <3cac0454-ceb2-449a-8b9a-1fac3f866a9c@scriptfusion.com> <9c763cee-6524-4423-b95e-6bfff4763d2e@app.fastmail.com> In-Reply-To: <9c763cee-6524-4423-b95e-6bfff4763d2e@app.fastmail.com> Date: Thu, 23 May 2024 08:35:09 +0200 Message-ID: Subject: Re: [PHP-DEV] [Discussion] Implicitly backed enums To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: landers.robert@gmail.com (Robert Landers) On Wed, May 22, 2024 at 4:13=E2=80=AFAM Larry Garfield wrote: > > On Tue, May 21, 2024, at 6:47 PM, Bilge wrote: > > On 22/05/2024 00:31, Larry Garfield wrote: > >> I could see an argument for auto-populating the backing value off the = enum name if it's not specified, something like this: enum Options: string = { > >> case First; // This implicitly gets "First" > >> case Second =3D '2nd'; > >> } > > This seems like a reasonable compromise. In this case, all I need to do > > is change my enum to a backed enum (suffix `: string`) and I get the > > benefits of implicit values. I still like the idea of the same being > > possible for non-backed enums, though I imagine that is a product of my > > na=C3=AFvet=C3=A9, as I do not tend to think of things in the framing o= f > > (de)serialization. > > Making it really hard and unnatural for people to use PHP enums as "cheap= named strings/ints" is something we want to keep, because the whole value = of enums is that they are their own separate type. A Direction is not a st= ring, it's not an int, it's not an array, it's not a Product, it's a Direct= ion. That's the end of it. Databases and HTML don't know from Direction o= r Product, though, so it has to get serialized down to something for those.= For arbitrary objects, you basically have to write your own mechanism. (= I did.) For enums, one comes built-in to make it more standardized: backed= enums. For what it's worth, the biggest downside to this decision is in upgrading old/legacy projects to use enums. Most of the time, you want to convert a const to an enum, and that also usually means it will be a backed enum. Since there's no way to cast a backed enum to a string/int, you have to instead add ->value (I really don't understand the difference; casting would have been much more elegant); this almost always results in lots of hidden runtime errors. At the last couple of places, after seeing the damage the conversion could do, doing this conversion was simply off-limits. Maybe by now, there is some reliable tooling to handle this automatically... I haven't looked. The point remains that it is a strange decision to use `->value` but not allow `(string)` or `(int)` or allow even implementing it yourself. > > >> I'm not sure if I'd support it myself at the moment > > Noted, but I once again find myself needing to ask: why not? Were it up > > to me, I'd say let's start right now! :) > > Mainly because I haven't thought it through to see what possible issues i= t could cause. It may be safe, or there may be currently-not-obvious issue= s that would result. That's the sort of thing we'd need to explore. > > For instance, how useful would it be, given that the casing for an enum s= hould be CamelCase (per PER-CS), but the serialized string is most often sn= ake_case, and otherwise lowerCamel? We definitely cannot bake case folding= magic into the behavior. So is it even useful at that point? I don't kno= w. Maybe. That's what needs to be explored. I don't see how casing of the backed value is relevant. If you want it to be something different, then specify it in the class body. 9/10 of the time, you just need to serialize the enum somewhere and the casing doesn't matter. If it does matter, then specify the values manually. > > It's not something I'm planning to work on myself at this point, though i= f someone else wanted to dig into it I'm happy to help brainstorm with them= . > > --Larry Garfield