Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128088 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 lists.php.net (Postfix) with ESMTPS id D8DA11A00BC for ; Wed, 16 Jul 2025 17:29:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1752686892; bh=l4UQFS6N+6/FU/KHt3NOLbp0dWYMKTXwwyuWmvYWzBI=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=Md1NBIQT0dzZfGQdbOBGSkqG+p4Dn+kB0pPB3BBnADvgCrpxN3OpYKTOrYNIlWdXr DHqJXjxKTxBaQtMo5JhdVaVYivykR1be1XFa5YxxrL2AArJwXkL8T+ZGtF1nn/ANO4 NMCPk0W3Lcvjuf4qOb6bfy7ZrGpFJmt1qip0MDsOafc8gOdS+LxWLc5wm1I+6MFAC5 QEE2w++JMhCHeQWOd2rihFtkjT2KvNJvsBNX0M5uhVbTG+AVDjqa0ETqk0eQyRC4dG FcjtC0UCzio9kasthIQZ8SlHQl/1ivEx/2eJmjHWpEcR/e/Se8y8F+gUX5GnwdQUAD 7OVvd5bOdb5aA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id F348A1801D4 for ; Wed, 16 Jul 2025 17:28:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_05,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.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fout-a3-smtp.messagingengine.com (fout-a3-smtp.messagingengine.com [103.168.172.146]) (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, 16 Jul 2025 17:28:10 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id 2D5ADEC017D; Wed, 16 Jul 2025 13:29:57 -0400 (EDT) Received: from phl-imap-05 ([10.202.2.95]) by phl-compute-05.internal (MEProxy); Wed, 16 Jul 2025 13:29:57 -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=1752686997; x= 1752773397; bh=v9yCZPUIxRhCWOtvJCnz8h4shgiu7kwzfyfvLpIfuJ4=; b=h zX4wnIt6NtRV5MU2CotHIcM07tD8OWxpMEKkIFQTPkeE6yu/psrp0Put+pUlsjpW skfoFjbZbVmTLFWKLwowsShHM9i0GAZNVGJYlW5ynp0W6RtKvWthCkt6WDF0PhG7 oNC4BRCktmMMuIIdSginNc61CYnhNvoXn+tq5paKc1koeK/zHnc8Up5X0euRitmB +egsFwsxWoBQa6rNVcJETPHD5s1+VwFDB3d/6n7TCOrh7qb+/5DVKvXG0gtVG8qr 9kJmhgqgzlYMq25xzJ3q/KXC2nYWAQnP5L9GscC1d71nyOqcJpYfTvYloQw0E5Wv MxOM29qEHCGTbaPFEkwqw== 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-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1752686997; x=1752773397; bh=v9yCZPUIxRhCWOtvJCnz8h4shgiu7kwzfyf vLpIfuJ4=; b=kz5iOzavorUJrJfAx+r0uAjOjR08QaMh2dQQPUfOJKDBGkW7JM+ cItiH42mlwWPjCKcGMoi13UxiSk0g+3Ps01gFAZVYqb3sVPDdZtpJ4nKjJMTxtcQ L7PSbZm134NPsEASdV3inrvcVmsyXiLB1b+wxmMXhPLC+XNH+Zk5EyolBWg2v77+ GO62Av1mXACN7qfINYY8hD3ZQpVu4Qs2/FTfVyb9flxYSLZaEeNnRToXECuuUSl5 yD5AKiPtoHqJgupdWDw1BDdHpg4TZicfIsc4kJUZVZ60xpLV519rxL3PBKM/ZNYj +NZe64iA4ECXw9gm9Hk2oDBP+kkmRRQlxrw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdehkeefvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefoggffhffvvefkjghfufgtsegrtderreertdejnecuhfhrohhmpedftfhosgcunfgr nhguvghrshdfuceorhhosgessghothhtlhgvugdrtghouggvsheqnecuggftrfgrthhtvg hrnhepieeuteehvddvfeejhffgieehleehhedthfefkeejffelgfevvdekudetjeejtddt necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprhhosg essghothhtlhgvugdrtghouggvshdpnhgspghrtghpthhtohepvddpmhhouggvpehsmhht phhouhhtpdhrtghpthhtohepuggvlhgvuhhghihnsehgmhgrihhlrdgtohhmpdhrtghpth htohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id C0ED41820074; Wed, 16 Jul 2025 13:29:56 -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 X-ThreadId: Tb5b4f3e5fa36e0f0 Date: Wed, 16 Jul 2025 19:29:36 +0200 To: =?UTF-8?Q?Marco_Aur=C3=A9lio_Deleu?= Cc: internals@lists.php.net Message-ID: In-Reply-To: References: Subject: Re: [PHP-DEV] Discussion Short Constructor Content-Type: multipart/alternative; boundary=a3226ad1db4342909824fb3cc03cb119 From: rob@bottled.codes ("Rob Landers") --a3226ad1db4342909824fb3cc03cb119 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Wed, Jul 16, 2025, at 17:44, Deleu wrote: >=20 >=20 > On Wed, Jul 16, 2025 at 3:49=E2=80=AFAM Rob Landers wrote: >> __ >>=20 >> Why I haven't proposed it as a separate RFC: with PSR-4 being so popu= lar, nobody is going to write one-line files to take advantage of this. = Thus, when I was going to revisit nested classes later this year (after = all the release shenanigans and some personal issues), I was planning to= add this feature to the nested class v2 RFC (again). A lot of feedback = I got on nested classes was "when would I use this?" -- and I suspect th= at would be a lot of the feedback you'd get here (see: PSR-4). I'm more = convinced than ever that short constructors and nested classes create a = chicken-and-egg problem. They only really make sense together; at least = with our current coding conventions and standards. It will create a long= er discussion period, but that's fine, IMHO. >>=20 >> If you'd like to continue with this RFC, I'd love to discuss it furth= er with you and help you out. I believe it is a bit more than "just" syn= tax sugar, but I'd have to dig out my records implementation. >>=20 >> =E2=80=94 Rob >=20 > I remember PHP from ~2000's and PSR-4 and Composer were probably one o= f the biggest improvements to the ecosystem. However, I feel like a "2.0= " version is long overdue. I have participated in some internals discuss= ions that point out a fact from the other direction: PHP makes no imposi= tion about file structure. 1-class-per-file is strictly a PSR-4 implemen= tation of a custom autoloader which we have gotten used to for so long. = I would really love to be able to declare small related classes/interfac= es in a single file much like we can do with Typescript, but the current= state of PHP "register your autoloader however you would like it" vs th= e current state of the community "PSR-4 is THE standard", we end up betw= een a rock and a hard place. I don't particularly enjoy nested classes a= nd how the syntax and its contexts get involved. Sure it would be nice t= o have private classes, but it gets quite cumbersome to have to do it wh= ile nesting classes within classes. I won't comment on the composer stuff or autoloading for that matter. Bu= t to your point about nested classes -- well, the entire point of nested= classes was to make it easy for encapsulation: class FooSelector { public function byId($id): array { /* select by Id */ } class AsAdmin extends FooSelector { public function byId($id): array { /* select by Id */ } } } $selector =3D $isAdmin ? new FooSelector\AsAdmin : new FooSelector; But where it really starts to get useful is when you can rewrite a retur= n by array, into a simple object: class FooSelector { class Foo(int $id, string $bar); public function byId($id): Foo { } } When working in established code, we very rarely write new classes when = compared to greenfield systems, where you have to write every new class = (data objects, services, controllers, repositories, etc). In greenfield = php projects, you often reach for an array until you need to create a pr= oper class. Instead of reaching for an array, or writing out an entire f= ile + boilerplate class, you can just write a short, tightly scoped clas= s... much like we used to do back before PSR-4. If you recall back then,= we'd often put it in the same file because we always knew that FooSelec= tor would always be included before you could get a Foo. (and yeah, it w= as a time bomb; it was only a matter of time before someone would create= a Foo before they created a FooSelector and it would crash) class Foo { /* */ } class FooSelector { /* */ } If you ever wanted to migrate it to a proper class, you'd just create a = folder (FooSelector) and copy/paste the class into a new file and keep P= SR-4 working just fine. In other words, trying to create a FooSelector\F= oo when Foo was nested, would properly autoload the correct file. Nested classes v2 won't include visibility. I think that just complicate= d things too much. But yes, in established code bases, they probably don't make as much sen= se, but that was the main problem I was trying to solve: the annoyance o= f choosing between an array -- or the boilerplate of creating a new clas= s just to hold a few fields. >=20 > On the other hand, we can see from constructor property promotion that= these little syntax sugar can be such a massive improvement to cognitiv= e load, readability, writability, etc. I would really love to have somet= hing much like this RFC even if at first I need to do it as a one-line-p= er-file. To me, the more interesting chicken-egg problem to address is w= hether PHP needs to provide some change to symbols, namespaces, autoload= er or if the community needs to expand PSR-4 in a way that allow us to n= ot reject this RFC on the basis of "PSR-4 will drastically limit syntax = sugar improvements". >=20 > -- > Marco Deleu I'd also love to see this get implemented and I've offered to help as mu= ch as I can. =E2=80=94 Rob --a3226ad1db4342909824fb3cc03cb119 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Wed, Jul = 16, 2025, at 17:44, Deleu wrote:


=
On Wed, Jul 16, 2025 at 3:49=E2=80=AFAM Rob Land= ers <rob@bottled.codes> wrote:


Why I haven't proposed it as a separate RFC: with PSR-4= being so popular, nobody is going to write one-line files to take advan= tage of this. Thus, when I was going to revisit nested classes later thi= s year (after all the release shenanigans and some personal issues), I w= as planning to add this feature to the nested class v2 RFC (again). A lo= t of feedback I got on nested classes was "when would I use this?" -- an= d I suspect that would be a lot of the feedback you'd get here (see: PSR= -4). I'm more convinced than ever that short constructors and nested cla= sses create a chicken-and-egg problem. They only really make sense toget= her; at least with our current coding conventions and standards. It will= create a longer discussion period, but that's fine, IMHO.
If you'd like to continue with this RFC, I'd love to discuss= it further with you and help you out. I believe it is a bit more than "= just" syntax sugar, but I'd have to dig out my records implementation.

=E2=80= =94 Rob

I remember PHP= from ~2000's and PSR-4 and Composer were probably one of the biggest im= provements to the ecosystem. However, I feel like a "2.0" version is lon= g overdue. I have participated in some internals discussions that point = out a fact from the other direction: PHP makes no imposition about file = structure. 1-class-per-file is strictly a PSR-4 implementation of a cust= om autoloader which we have gotten used to for so long. I would really l= ove to be able to declare small related classes/interfaces in a single f= ile much like we can do with Typescript, but the current state of PHP "r= egister your autoloader however you would like it" vs the current state = of the community "PSR-4 is THE standard", we end up between a rock and a= hard place. I don't particularly enjoy nested classes and how the synta= x and its contexts get involved. Sure it would be nice to have private c= lasses, but it gets quite cumbersome to have to do it while nesting clas= ses within classes.

I won't = comment on the composer stuff or autoloading for that matter. But to you= r point about nested classes -- well, the entire point of nested classes= was to make it easy for encapsulation:

class F= ooSelector {
  public function byId($id): array { /* sele= ct by Id */ }
  class AsAdmin extends FooSelector {
=
    public function byId($id): array { /* select by Id */= }
  }
}

$selector =3D= $isAdmin ? new FooSelector\AsAdmin : new FooSelector;

But where it really starts to get useful is when you can rewrite= a return by array, into a simple object:

class= FooSelector {
  class Foo(int $id, string $bar);

  public function byId($id): Foo { }
}<= /div>

When working in established code, we very rarel= y write new classes when compared to greenfield systems, where you have = to write every new class (data objects, services, controllers, repositor= ies, etc). In greenfield php projects, you often reach for an array unti= l you need to create a proper class. Instead of reaching for an array, o= r writing out an entire file + boilerplate class, you can just write a s= hort, tightly scoped class... much like we used to do back before PSR-4.= If you recall back then, we'd often put it in the same file because we = always knew that FooSelector would always be included before you could g= et a Foo. (and yeah, it was a time bomb; it was only a matter of time be= fore someone would create a Foo before they created a FooSelector and it= would crash)

class Foo { /* */ }
class FooSelector { /* */ }

If you = ever wanted to migrate it to a proper class, you'd just create a folder = (FooSelector) and copy/paste the class into a new file and keep PSR-4 wo= rking just fine. In other words, trying to create a FooSelector\Foo when= Foo was nested, would properly autoload the correct file.
Nested classes v2 won't include visibility. I think that jus= t complicated things too much.

But yes, in esta= blished code bases, they probably don't make as much sense, but that was= the main problem I was trying to solve: the annoyance of choosing betwe= en an array -- or the boilerplate of creating a new class just to hold a= few fields.


On the other hand, we can s= ee from constructor property promotion that these little syntax sugar ca= n be such a massive improvement to cognitive load, readability, writabil= ity, etc. I would really love to have something much like this RFC even = if at first I need to do it as a one-line-per-file. To me, the more inte= resting chicken-egg problem to address is whether PHP needs to provide s= ome change to symbols, namespaces, autoloader or if the community needs = to expand PSR-4 in a way that allow us to not reject this RFC on the bas= is of "PSR-4 will drastically limit syntax sugar improvements".

--
Marco Deleu

I'd also love to see th= is get implemented and I've offered to help as much as I can.
=
=E2=80=94 Rob
--a3226ad1db4342909824fb3cc03cb119--