Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126526 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 E207F1A00BC for ; Fri, 28 Feb 2025 17:14:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1740762710; bh=pV2v1LkbYvt9dkLD6fhkrJYGm6qQP4OuTjuArLjIY1k=; h=Date:Subject:To:References:From:In-Reply-To:From; b=n4AwkmVci+n+QMNg23z7BH9JBgC5x5vNtkGb0SP6TOras1W6Auqko2mdYJY1Rkgz3 h2XOiJRUvLAODEScQagQIGm5anEbcwzwOJX/po8vsf56Qblnqs+jCuUj4X/Y3DCMpC QkuP4V9bz/CsUV5TddREYPlU+0hmgIjR0SHfP9XsC2kr+NUJMTX6pWe9a50pByJXtX bAvoOmEOt0RgfBDoxnoUUDMIHOXWvk+kTSy7IoB8jr6sirNn6Oz7pWWwJBFUzplbMj KvhsC6swWQQEMvRrsAuk1xWyVtIN8DsiJCCOXWw8Zv7h2cZulIS1iQrbRc6cUH8tj0 S6M+0YDJ7SNtg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 79387180034 for ; Fri, 28 Feb 2025 17:11:49 +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=-4.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b5-smtp.messagingengine.com (fhigh-b5-smtp.messagingengine.com [202.12.124.156]) (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, 28 Feb 2025 17:11:49 +0000 (UTC) Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfhigh.stl.internal (Postfix) with ESMTP id 2D5472540109; Fri, 28 Feb 2025 12:14:26 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-12.internal (MEProxy); Fri, 28 Feb 2025 12:14:26 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=allenjb.me.uk; h=cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1740762866; x=1740849266; bh=yfP4P5bLdVRZk0FnUZBCudV6xzSkNH5fjKmFDtzKr+k=; b= USC2/+VgUBCURjOKwPZcRAWymLqRWRaHcem6Wqx/2j8JdIf+VwUi+IPZr7R4diok RnE0GwOoJYFe9VGsb+uj/XXFnReCSckrVxT4AQVoQgVy11tZR/yUQD63va83oHEi DeW5N9GFhrO28HVvZ5pqKxHFDEQdeWIFXFpiHMZM91HiLQp9ffHeTHUsOdSSuedf v1tu4G5MZcbJEMoef2KohtipFBw5jWl4/g0OeJQN44pPkakX5DWXQZ3C5gPQAu7U Q9EvJahcLip8c5ODUWlDXDvL+CWRWZMHLVYHzpFuxKCok0llq0NZlgdni/FkODDG AzJtexxDIJ13s4iXKMPJAw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1740762866; x=1740849266; bh=y fP4P5bLdVRZk0FnUZBCudV6xzSkNH5fjKmFDtzKr+k=; b=ZrIBTFR8eYkD+ScUU vCACbKQijyND0gGS5UPGYY6DfCNdUtU1OBawO0ozQtWPmAjxEAqASSNwfape705b pzX0FGURT0m9tpz7BsonxyKTq7MevZBlSAJXzFxroumNeWIn5XnF5swXsK8ERAl/ O2lOnd/j3KQDcnj7NxS3oykdIIZ+vWLVbnEbrn8/ib856EgK3H7oLQWqF52owPSr G/4SdyyQjnJ9kQK5g/GX85gDwHPavF0lD48dZQ6XMTeSbnbQ15696FsWPcACfZPB phjUrcwhUE4IxrI/FjnEG7vv60qaTMx1hkE++zYjI8RJkKOwsjZCuDa0NDF9Wjpc ewVBQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdeltdeliecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtjeertddtvdej necuhfhrohhmpeetlhhlvghnlfeuuceophhhphdrlhhishhtshesrghllhgvnhhjsgdrmh gvrdhukheqnecuggftrfgrthhtvghrnheplefhueekjefghfeuheejleevhfdtiedtuedv teejkeefieektdefgfeludevvdeunecuffhomhgrihhnpehphhhprdhnvghtnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhhphdrlhhishht shesrghllhgvnhhjsgdrmhgvrdhukhdpnhgspghrtghpthhtohepvddpmhhouggvpehsmh htphhouhhtpdhrtghpthhtohepjhgrnhhishdrkhgrjhgrkhhssehgmhgrihhlrdgtohhm pdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: idf69468d:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 28 Feb 2025 12:14:25 -0500 (EST) Message-ID: <130e71e3-830c-4521-bbed-89084fc981d7@allenjb.me.uk> Date: Fri, 28 Feb 2025 17:14:24 +0000 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] Enums should implement Stringable interface by default To: Janis Kajaks , internals@lists.php.net References: Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit From: php.lists@allenjb.me.uk (AllenJB) So to summarize the proposed behavior (as I understand it): * If an enum is backed, it auto-casts to ->value * If an enum is not backed, it auto-casts to ->name I believe this has the following problems (at least): * Unbacked enums are not supposed to be representable as scalar values - if I, as a developer, intended an enum to be representable as a scalar, it would be backed; * Different behavior depending on whether the enum is backed or not is confusing; * If an unbacked enum is created, then later a developer wants to change it to backed, it can break code / change output in unexpected, and in ways that are very difficult to search for. You need to, at least, not just search for usages of the enum, but usages of the properties/variables which can be that enum (and repeat recursively for further assignments), then work out which of those might involve auto-casting. (This sort of change probably isn't very common, but that doesn't mean it should be made a nightmare when someone does want to do it) I don't think enums should ever auto-cast to scalar values (backed or not) Venturing further down this road starts getting into what I suspect is dangerous territory, with obvious examples being: * enums as array keys (which I would like to be able to do, but not via this sort of auto-casting) * comparing different enums with the same backed values (or even different unbacked enums with the same ->name) And that's without looking at potential conflicts this behavior would bring with planned features such as https://wiki.php.net/rfc/adts My personal opinion on the current json_encode() behavior with backed enums here (and anywhere else where's there's similar auto-casting behavior) is that it's undesirable and it should be an error. I think it's completely ambiguous as to what the author intended (or if they even intended to at all) when representing a backed enum in JSON. If you do want this behavior in your application in cases such as json_encode(), it's pretty easy to write a wrapper or helper that casts values as desired. I've done this with database level code (eg. casting bool to 1/0 for MySQL, as a simple example).