Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126805 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 6E5A51A00BC for ; Mon, 17 Mar 2025 15:30:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1742225275; bh=80BUKG6XrBbuc8zMKPNNZA21wXydIkGrv9x6YyaBPXM=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=dramx0ZC6u3D6jv5Z7+iguBj/YQAXU8ebWb3YK6MdESDO69vPc+OD8KxSwxhgIZM9 VXx9lbREKh0zVHbBSRXsflQ/Azkqt9Ok7rTQZQetTFueinniGeSX46YhqbU9lXsn2g qgZPCL25+bpC6Rf1pB1JN3UwXyWgPGvk+MmwEs1mArwOoFvOJgkYYhmd0XKG2VxJj0 ks1Ek0CbzHIRxDMb5MLNEUT3Px9YDVnM1sWG6v9iKN+IaFObmHI+DTrfX7Vi7OWtv4 KkVx9bfEt+701Mzt1MGvGbVpJTFGLrzDpm+a3FPLNKicFlTt8Ww6RpB564b7/xf3fK zYRBzsDseURNQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 610A518007E for ; Mon, 17 Mar 2025 15:27:54 +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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 ; Mon, 17 Mar 2025 15:27:51 +0000 (UTC) Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-22548a28d0cso124808005ad.3 for ; Mon, 17 Mar 2025 08:30:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742225421; x=1742830221; 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=D6z7koKttGMLuXCl7h3W0p4DBRMeyxxO6Fb3wMtnhAE=; b=KZB97UEB9lS58nOmA4KdhizDiEySYUeZeFF95AHQGCzhnFyZv3yhguCbjx+sESrXXX pVBZ12xysvisAKcCQl8poWUUeETOdW7hhhreSeBpHUqchSPsg2tltI5gB0x9YSUxlRVJ ZYUpZftXcGfKwnbp4fgudur8CYR9DLEVDrTyrjOt9ouVYZRWx2iC3yJRVvOCcLzfScE+ RmDFZkfoSp9Cxdj1jkPrRvIydg1826nMm1gQYjfyZnc8KcB0LWg7jIMpAOIRldmsJnCP ZpX7GWzI/FBM5tiU+c6e6Puad1F46ZDljLfXs8sy49eZ9P+fNWTBrctaWoDPz/l2hhLq cTkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742225421; x=1742830221; 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=D6z7koKttGMLuXCl7h3W0p4DBRMeyxxO6Fb3wMtnhAE=; b=NrVS5NS5HI7fmBEB757pcA2Pa07dfIf7C/692vmbtBHXeaKxbbmF5w5p+rVCMxCvBY vs2RmaQD+TsRKEn6SLUHVwEgkXjbxw898Khotm1V9RUjPOlivT9+7xqinqte9MEwStRu lJ5vL4XUIhvT0TTwqRWPj0ppbriazSZGAh7fxhSWgXCJ0AkNcEGjvsuXUzKF3ffikhCU mFhwoFIrO7w7FMXBtDz53M3dXRCxBO9L6STA5eBXl+ezmBV/3nI+Bal+e4BbOUM1q9rf hL2R5AFPcqaziq67lxzO9CPQYRzxPvkL1D3zuYN6VaAL7GvMeD4f32SPN6ei3MCGzcO7 jWIg== X-Gm-Message-State: AOJu0YxNmhoSBXSGCNSbSh8R0f/Wk+PdgVSS2Dlzmx2+u9kmGtIpSfO4 1BF2cdn1f4XYMbtN0STmcejR6BjMfDjyaDPqc5/1kOJ7zVc3awncN4wN7pD6xWy3+e/5dZ9oTTE JvL1mOuubBdPX08iojJ5eZuwN37kFWo4d X-Gm-Gg: ASbGncukgA+ANX+KUJCw2nbNaluwbjWEDNuF5NlobppcJAmDyTTssgmWWiTPAk39Fvt MTgpLSx243T6dQmyfpxK0UvKn3ZvADZxoW27JfZDKOl+YSlBaLWpX5nLCnvAV1rTsmJ5kMGfbbr GQ5g5vKZfBYh/qD2pqlGSBLde9tA== X-Google-Smtp-Source: AGHT+IGa0FQyH1VHWUQXp2dzihU1SuTEK9ej4XKipKmo2sJhN9U5yl8Jy1MSh00WL5bYwF63LuRJcxSyvDVcrMxwGS8= X-Received: by 2002:a17:902:d50b:b0:224:76f:9e45 with SMTP id d9443c01a7336-225e0a5bdb1mr165182705ad.21.1742225421431; Mon, 17 Mar 2025 08:30:21 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 17 Mar 2025 15:30:28 +0000 X-Gm-Features: AQ5f1JpE7tBoJVYZCfJ2KAnqRKcufopFXFT5b-zO75UJdRYfTRtPA2M1jM2bv6M Message-ID: Subject: Re: [PHP-DEV] RFC: short and inner classes To: Rob Landers Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000005d019506308b77d3" From: fenniclog@gmail.com (fennic log) --0000000000005d019506308b77d3 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, 5 Mar 2025 at 23:14, Rob Landers wrote: > Hello PHP Internals, > > I'd like to introduce my RFC for discussion: > https://wiki.php.net/rfc/short-and-inner-classes > > This RFC defines a short class syntax as well as the ability to nest > classes inside another class. This introduces an unprecedented amount of > control, flexibility, and expressiveness over how objects are used and > instantiated in PHP. There is a PR ( > https://github.com/php/php-src/pull/17895) that implements this > functionality -- all test failures are related to different/new/incorrect > error messages being generated. However, the core functionality exists to > take for a test ride. > > So, what do I mean by "unprecedented amount of control"? With this change= , > you can declare an inner class as private or protected, preventing its > usage outside of the outer class: > > class User { > private class Id {} > > public function __construct(public self::Id $id) {} > } > > In the above example, the class `User` is impossible to construct even > though it has a public constructor (except through reflection) because > User::Id is private; User::Id cannot be instantiated, used as a type hint= , > or even via `instanceof` outside of the User class itself. This example > isn't practical but demonstrates something that is nearly impossible in > previous versions of PHP, where all classes are essentially publicly > accessible from anywhere within the codebase. > > As a number of inner classes will probably be used as DTOs, the RFC > introduces a "short syntax" for declaring classes, which enhances > expressiveness, even allowing the usage of traits, all in a single line: > > // declare a readonly Point, that implements Vector2 and uses the > Evolvable trait > readonly class Point(public int $x, public int $y) implements Vector2 use > Evolvable; > > When combined with inner classes, it looks something like this: > > class Pixel { > public readonly class Point(public int $x, public int $y) implements > Vector2 use Evolvable; > } > > // Create a new pixel point with property $x and $y set to 0 > $p =3D new Pixel::Point(0, 0); > > There are far more details in the RFC itself, so please check it out. I'm > quite excited to hear your thoughts! > > =E2=80=94 Rob > > PS. I know I tend to rush into things, but I want to make it clear that > I'm not rushing this -- I've learned from my mistakes (thank you to those > who have given me advice). I'm going to do this right. > I have read and reread this RFC, and I am struggling to see 1. What problem does this solve that anonymous classes do not? 2. As with any syntax change and new operator there needs to be very careful consideration, do we need a new operation, or could `::` if the parent is static or `->` if the class is initialized? 3. The idea that extending the parent class doesnt no inherit the child classes doesnt make sense to me. As then if you extend a parent class and call a function of that class which could rely on the existence of an inner class, I can see a lot of headaches caused by this exact scenario. As a developer, if I extend a class, I expect the entire dependance of that class to be inherited, otherwise the extending class won't work. --0000000000005d019506308b77d3 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Wed, 5 Mar 2025 = at 23:14, Rob Landers <rob@bottled.codes> wrote:
Hello PHP Internals,

I'= ;d like to introduce my RFC for discussion:=C2=A0https://wiki.php.net/r= fc/short-and-inner-classes

This RFC define= s a short class syntax as well as the ability to nest classes inside anothe= r class. This introduces an unprecedented amount of control, flexibility, a= nd expressiveness over how objects are used and instantiated in PHP. There = is a PR (https://github.com/php/php-src/pull/17895) that implements this f= unctionality -- all test failures are related to different/new/incorrect er= ror messages being generated. However, the core functionality exists to tak= e for a test ride.

So, what do I mean by "= ;unprecedented amount of control"? With this change, you can declare a= n inner class as private or protected, preventing its usage outside of the = outer class:

class User {
=C2=A0= private class Id {}

=C2=A0 public function __= construct(public self::Id $id) {}
}

= In the above example, the class `User` is impossible to construct even thou= gh it has a public constructor (except through reflection) because User::Id= is private; User::Id cannot be instantiated, used as a type hint, or even = via `instanceof` outside of the User class itself. This example isn't p= ractical but demonstrates something that is nearly impossible in previous v= ersions of PHP, where all classes are essentially publicly accessible from = anywhere within the codebase.

As a number of i= nner classes will probably be used as DTOs, the RFC introduces a "shor= t syntax" for declaring classes, which enhances expressiveness, even a= llowing the usage of traits, all in a single line:

=
// declare a readonly Point, that implements Vector2 and uses the Evol= vable trait
readonly class Point(public int $x, public int $y) im= plements Vector2 use Evolvable;

When combined = with inner classes, it looks something like this:

= class Pixel {
=C2=A0 public readonly class Point(public int $= x, public int $y) implements Vector2 use Evolvable;
}

// Create a new pixel point with property $x and $y s= et to 0
$p =3D new Pixel::Point(0, 0);

There are far more details in the RFC itself, so please check it out. I&= #39;m quite excited to hear your thoughts!

=E2=80=94 Rob

PS. I know I tend to rush= into things, but I want to make it clear that I'm not rushing this -- = I've learned from my mistakes (thank you to those who have given me adv= ice). I'm going to do this right.
<= div>
I have read and reread this RFC, and I am struggling to = see=C2=A0

1. What problem does this solve=C2=A0tha= t ano= nymous classes=C2=A0do not?
2. As with any syntax change and = new operator there needs to be very careful consideration, do we need a new= operation, or could `::` if the parent is static or `->` if the class i= s initialized?
3. The idea that extending the parent class doesnt= no inherit the child classes doesnt=C2=A0make sense to me.=C2=A0
As then if you extend a parent class and call a function of that class whi= ch could rely on the existence of an inner class, I can see a lot of headac= hes caused by this exact scenario.
As a developer, if I extend a = class, I expect the entire dependance of that class to be inherited, otherw= ise the extending class won't work.=C2=A0
--0000000000005d019506308b77d3--