Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125736 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 5E8D01A00BD for ; Wed, 2 Oct 2024 20:02:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1727899490; bh=62cLN+lYVhfgKe8WjiIrnLtuXwrtyUCp4cSq9zXuhVU=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=fLK89nNim6ZqrSDSeAwkp54PP6TtC8t1n1E5T7j4WbH7pnUr3phHeHbmNVinJkbTw y7ANlAY7R8oA2/Mx3JZMgQrcab3+ZhHrNh++9a1heZ2fvE4ZKhgIgNmyhoJNH8fNPJ IUvpY0ona4TAb84nDLH7AildLxjpgD0EnzqTIfMdD29mJQcwIM5pw3RFlyiLZ1CJel LqMRU1KY74mQ95jnTpf0lXWFG4+uGWFiKvN2EM1cdPDtTaOHtKQ4rphBtAYmq8ZH+v erHF3a7lKgOWvLK7uw6YSLIFLM0Hohp7jWmYwO2dqt+7Un0CfqpTCHYhF0MaXu4zeY G7SHXdtU5URIg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 48C2C18006D for ; Wed, 2 Oct 2024 20:04: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=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout-a6-smtp.messagingengine.com (fout-a6-smtp.messagingengine.com [103.168.172.149]) (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 ; Wed, 2 Oct 2024 20:04:48 +0000 (UTC) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfout.phl.internal (Postfix) with ESMTP id 9BF841380225; Wed, 2 Oct 2024 16:02:34 -0400 (EDT) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-01.internal (MEProxy); Wed, 02 Oct 2024 16:02:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:cc: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=1727899354; x= 1727985754; bh=mxQJfrgPFGf1tMZgx07c42rNfnHxPmTMrTZuEiH+Iio=; b=n 32v0agsv/DW1HlsCpgu0Zg11P/XQlZHZryVon9mHlwcx6478XLwN6vnW/yti0mch TFrPooUdjKheX39JxP1fhm8qdBv4J1wrHSUSMcn8EBwja7EsUXnp2+DCcpYm4UIV ggGg6UxqAXnTV2jlsi5X0N5Anwi82rmy+oAfGiTxNRK0uGHhTW60RVo0oYi/245H bB/2F5ThwgZPhmBpEmUMsDuxmeq/6aYFfTb9a5ANpGRg+IPl1O49X/UKz0qE6TYL DKE0hNq01ThdcF4BUq2jNaV+C+Vq2sMru5suY/uB1qehj5ny4W4yVm5ql7JmT8PW EzwoGtv42nstSIWZEtHHQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc: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-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727899354; x=1727985754; bh=mxQJfrgPFGf1tMZgx07c42rNfnHx PmTMrTZuEiH+Iio=; b=LtQaFjDsaw++obj7y0ClS4xJzFkcyg1Kt4bi9f/McpRD 42wLPMVn9BkUdAVJveg+U5E4oVFGkzoQi+BKIAua5m62tRmycCYq23hbh6BJykMS Yw9nccot6HpvOWF6c0eqozidvuKAkcn47iQ/pHW0lniN34BteIpRzVnr2heeAUtH 2K+UwUliWt3SbNlWh+NL98dqOQuLQ1u1gDIFKxiWD0luJQNMj2By6A2b3TtJu47M 9WFURwzKEn4GbCjy1GlFGUQkm76dH+EkRl7tHpHz2/5j4SBBYh3vejQzuXOrXXCk PJY/U1OLNcFsI/14MsdqJx143zOS14TE8XH6d9P5nA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgudegudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenogfuuhhsphgvtghtffhomhgrihhnucdlgeelmdenucfjughr pefoggffhffvvefkjghfufgtsegrtderreertdejnecuhfhrohhmpedftfhosgcunfgrnh guvghrshdfuceorhhosgessghothhtlhgvugdrtghouggvsheqnecuggftrfgrthhtvghr nhepleeuudetfedvhedvgfelfefhveekfefhhfejgedtteevuddtkeevhfevheeivddune cuffhomhgrihhnpehphhhprdhnvghtpdefvheglhdrohhrghdpghhoohhglhgvrdgtohhm necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprhhosg essghothhtlhgvugdrtghouggvshdpnhgspghrtghpthhtohepgedpmhhouggvpehsmhht phhouhhtpdhrtghpthhtohepphhhphdqihhnthgvrhhnrghlshgpnhhoshhprghmsegrug hvihgvshgvnhiiohdrnhhlpdhrtghpthhtohepughrvggrlhgvtghssehgmhgrihhlrdgt ohhmpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvthdprh gtphhtthhopegrhigvshhhsehphhhprdifrghttghh X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id BDEFA780068; Wed, 2 Oct 2024 16:02:33 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Wed, 02 Oct 2024 22:02:13 +0200 To: =?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?= Cc: "Juliette Reinders Folmer" , "PHP internals" , "Ayesh Karunaratne" Message-ID: <31d49cf4-9e22-4e15-8589-ffc95a4ada82@app.fastmail.com> In-Reply-To: References: <66FC7B9F.5070906@adviesenzo.nl> Subject: Re: [PHP-DEV] [RFC] [Discussion] Add get_declared_enums() function Content-Type: multipart/alternative; boundary=664b6b4cbba74bde8ac8d29fbcb299fa From: rob@bottled.codes ("Rob Landers") --664b6b4cbba74bde8ac8d29fbcb299fa Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Wed, Oct 2, 2024, at 14:27, Alexandru P=C4=83tr=C4=83nescu wrote: >=20 > On Wed, Oct 2, 2024 at 11:06=E2=80=AFAM Rob Landers wrote: >> __ >> On Wed, Oct 2, 2024, at 09:20, Alexandru P=C4=83tr=C4=83nescu wrote: >>>=20 >>> On Wed, Oct 2, 2024 at 1:48=E2=80=AFAM Juliette Reinders Folmer wrote: >>>> L.S., >>>>=20 >>>> After the earlier discussion [1] regarding this topic in August, it= is our pleasure to present the RFC to add a `get_declared_enums()` func= tion to PHP for discussion. >>>>=20 >>>> https://wiki.php.net/rfc/get_declared_enums >>>>=20 >>>> We look forward to your feedback and hope for a constructive discus= sion. >>>=20 >>> I'm all for `get_declared_enums()`. >>> However, I don't accept that enums are not classes and going with th= e assumption that they are not will bring more problems than it solves. >>>=20 >>> I noticed how you analyzed things internally, that everything is a c= lass internally, including an interface or a trait. >>> But this is an API for the userland, and in userland this is not tru= e. >>> The 3 structures: classes, interfaces and traits and distinct things= that you can use in different ways in (generated) code. >>>=20 >>> I believe that in userland, the common understanding is that enums a= re just a specific type of class, final ones. >>> I would find it surprising if `get_declared_classes()` would not ret= urn enums. >>>=20 >>> Avoiding a BC break would be nice also if there is no reason for it. >>> One would be able to get only the enums by using something like: >>> ```php >>> function get_declared_enums_only() { >>> return array_diff(get_declared_classes(), get_declared_enums()); >>> } >>> ``` >>>=20 >>> --=20 >>> Alex >>>=20 >>=20 >> Interesting observation Alexandru, >>=20 >> I find it weird that class_exists("MyEnum") would return true, but th= ere is only one symbol table=E2=80=94you can=E2=80=99t name a class and = enum with the same name, so it makes some sense. However, while enums ha= ve =E2=80=9Csome=E2=80=9D similarities with classes in PHP, they are not= the same. For example, you cannot implement arbitrary interfaces (ie, S= tringable), you cannot extend another class, you cannot instantiate them= with =E2=80=9Cnew=E2=80=9D, you cannot have state, you cannot clone the= m, magic methods are forbidden, etc. >>=20 >> You cannot build a =E2=80=9Cclass=E2=80=9D with these rules in PHP, s= o I don=E2=80=99t think enums quack sufficiently like classes to be call= ed a class. I would argue that it is an object, however. I think it woul= d be worth implementing a get_declared_objects() that behaves like get_d= eclared_classes() currently does.=20 >>=20 >> =E2=80=94 Rob >=20 > Yes, that's a good point as well. >=20 > I think my view comes from when I initially dug (15+ years ago) into w= hat an enum in Java is, and learned that it is just a syntactic sugar, a= nd a final class would be generated implementing `Comparable` and extend= ing an abstract class `Enum`. >=20 > And I think in PHP that could be a similar view. And most of the limit= ations we have are not impossible to create with a standard class. >=20 > I think in time we might get to remove some limitations. > I, personally, don't agree with the `Stringable` related limitation. > And also with the limitation on no state. In Java this is the simple w= ay to create a singleton: an enum with one case; and we can't have this = in PHP. >=20 > If we remove the limitations, should we reclassify at that point enums= as classes? >=20 > --=20 > Alex > =20 Hey Alex, IMHO, the only thing keeping an enum from being "sugared classes" is the= ability to define state. I'm not even sure why it is a limitation becau= se it is relatively easy to "hack it in": https://3v4l.org/nqZfK=20 Being able to define state and behavior are fundamental parts of what ma= kes a class a class. If you can't have state, then I'm not sure it is a = class. We're getting into theoretical computer science here, but that is= , I think, a generally accepted basic definition in Object Oriented Prog= ramming.[1] The rest could be explained as just sugar for: 1. final class 2. extend base enum class (hence why it can't extend another class) 3. final clone method in base class which prevents cloning 4. hidden magic method implementations that throw (hence unable to impl= ement stringable) 5. singletons for each case 6. etc (and before someone argues that it "isn't how it works", I am merely des= cribing the sugar) So, yes, once that restriction is lifted, I think we could officially ca= ll them classes. Until then though, I think they're just "enums?" On Wed, Oct 2, 2024, at 14:19, Derick Rethans wrote: > On 2 October 2024 09:06:20 BST, Rob Landers wrote: >=20 > >You cannot build a =E2=80=9Cclass=E2=80=9D with these rules in PHP, s= o I don=E2=80=99t think enums quack sufficiently like classes to be call= ed a class. I would argue that it is an object, however. I think it woul= d be worth implementing a get_declared_objects() that behaves like get_d= eclared_classes() currently does.=20 >=20 > Objects are instantiated classes, and not declared elements, so that n= ame wouldn't make sense. >=20 > cheers > Derick=20 >=20 This is also why I suggested calling them objects instead of classes. En= um cases are instantiated enums which are objects, but they aren't class= es. We could be more specific and call it get_declared_object_types() I'd absolutely love to hear a reason, rooted in computer science, why PH= P enums are classes. I'm no expert on the subject, but this has bothered= me for years... so I would love to learn more or discuss the topic furt= her. [1]: https://books.google.com/books?id=3D9NGWq3K1RwUC&pg=3DPA18 Foundati= ons of Object Oriented Languages: types and semantics, Bruce, 2002 -- ju= st had to break out an old text book :) =E2=80=94 Rob --664b6b4cbba74bde8ac8d29fbcb299fa Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable