Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126589 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 2C8D91A00CD for ; Wed, 5 Mar 2025 23:11:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1741216153; bh=w5LTfLWAnUgiu24bMaVFrBGovxdFSQzR8w7mJJZ7huQ=; h=Date:From:To:Subject:From; b=UG36AqWzp/+QwFRRJaKs4S6Czwe/DLc6I7+SR3oY3v6LrJdVyTEJGyW8PReHIrPUc /k8CfYcBhuU1+Sm9QPQ4kkEAcBkKm8xxGrGprf6FmG6ntvFptSL0KHKXF1nf8iTiRJ VQT3/kkWNvbReyTul8alriH24UjI3sdtFE19jt2k9FGanYq0Y9qcfiBY69m4V/3eqC PIPNmyCNkSkdLpX9WhSxu78zKanfFiZw0jAWHIBwhu2UXcb+swg+SCvr+szJAEqFuW LM6v037ueHr4fGUYdi+ixvcf8iwi7qKkzsdB13u418GGcPEOSlU1C6st14kfC5DzlF Iul5zOFYVXzsg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E85C71801D5 for ; Wed, 5 Mar 2025 23:09:09 +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.8 required=5.0 tests=BAYES_00,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-a7-smtp.messagingengine.com (fout-a7-smtp.messagingengine.com [103.168.172.150]) (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, 5 Mar 2025 23:09:07 +0000 (UTC) Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfout.phl.internal (Postfix) with ESMTP id 1C69A13826ED for ; Wed, 5 Mar 2025 18:11:43 -0500 (EST) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-12.internal (MEProxy); Wed, 05 Mar 2025 18:11:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:subject:subject:to:to; s=fm2; t=1741216303; x=1741302703; bh=mJXEr7tbZEMsRbVnJ0/xC+XWYPr65XQg 2VePsMZGHlc=; b=QzXzki3Igcg7nFKsdQsb3Zb0yYX4IDpfV+SpkqZsTMjYW9k1 sDweG1ensRLdSZyFDbOYxTb3LV/1Or9qrms97Z/ccHJQtDS84rp6+HfcMIXZyN6+ RYhkBZtKZy9CaNR8UO1cBt+A8N2iAl/SguKFmKv+zxXgr8nxCf201K0tWSWO5jWA Vjkjg+OYdvFzDbb2wvpnRDk2zuwJoqg1WKFmRHpgtelwWbIx6hjHCoG4a3dRuCst hj/v+xA/2IL4hnVPxo75ANbdx8ElWNoGW+oHisTGGri2Z+zr/O7wnznOw/jbSnkK KTjWCXlPLw28npUeGdUjPipmxWehLjTnJL9CKg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1741216303; x= 1741302703; bh=mJXEr7tbZEMsRbVnJ0/xC+XWYPr65XQg2VePsMZGHlc=; b=c D8VV98wOC87iGo+C+SReuHc4Xz9E4+UyashtVlPwlnyZ3JQH0FPnC6imBWWQ4DpY YjzM9qPkxe0AXRZsfFiyiNkOBC7nkRA5Yst75NKcZaze77R74aB9QN4hPjBfrlG9 XJ9smRvqDV3YIHUysGBbaATyhsk3adT8CPAL5PPWYdFDaJ2VrtxojxxA+5CoYhio tpeOc0ZfXgHkxpFZxWfJp6wIiMAmgnQyXZdG9+V7pPI0zpIYrNgweLYNKLA9Uo72 4VT7hxDqI6XVc3OloIHT28qV7QG/23AhrUBZ77fQeVxJwlvf2iNDCpg6Xrjn8LKY ago+w94XGZCBbM3o+0L2Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddutdeiudefucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefogg ffhffvkffutgesrgdtreerredtjeenucfhrhhomhepfdftohgsucfnrghnuggvrhhsfdcu oehrohgssegsohhtthhlvggurdgtohguvghsqeenucggtffrrghtthgvrhhnpeefkeefge dvtdfgtedtvefffffffeevkeeggffgjeelueekieduvdegjeehkeelieenucffohhmrghi nhepphhhphdrnhgvthdpghhithhhuhgsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd enucfrrghrrghmpehmrghilhhfrhhomheprhhosgessghothhtlhgvugdrtghouggvshdp nhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnth gvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id B3CF2780068; Wed, 5 Mar 2025 18:11:42 -0500 (EST) 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: Thu, 06 Mar 2025 00:11:22 +0100 To: internals@lists.php.net Message-ID: Subject: [PHP-DEV] RFC: short and inner classes Content-Type: multipart/alternative; boundary=ffe53bd37c3d4c8ba370ca23dcc1c6d5 From: rob@bottled.codes ("Rob Landers") --ffe53bd37c3d4c8ba370ca23dcc1c6d5 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello PHP Internals, I'd like to introduce my RFC for discussion: https://wiki.php.net/rfc/sh= ort-and-inner-classes This RFC defines a short class syntax as well as the ability to nest cla= sses inside another class. This introduces an unprecedented amount of co= ntrol, flexibility, and expressiveness over how objects are used and ins= tantiated in PHP. There is a PR (https://github.com/php/php-src/pull/178= 95) 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 chang= e, you can declare an inner class as private or protected, preventing it= s 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 t= hough it has a public constructor (except through reflection) because Us= er::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 ac= cessible from anywhere within the codebase. As a number of inner classes will probably be used as DTOs, the RFC intr= oduces a "short syntax" for declaring classes, which enhances expressive= ness, even allowing the usage of traits, all in a single line: // declare a readonly Point, that implements Vector2 and uses the Evolva= ble trait readonly class Point(public int $x, public int $y) implements Vector2 us= e Evolvable; When combined with inner classes, it looks something like this: class Pixel { public readonly class Point(public int $x, public int $y) implements V= ector2 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 thos= e who have given me advice). I'm going to do this right. --ffe53bd37c3d4c8ba370ca23dcc1c6d5 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Hello PHP Inter= nals,

I'd like to introduce my RFC for disc= ussion: 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 co= re 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, prevent= ing its usage outside of the outer class:

c= lass User {
  private class Id {}

<= /div>
  public function __construct(public self::Id $id) {}
}

In the above example, the class `U= ser` 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 a= ll 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 decla= ring 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 Vec= tor2 use Evolvable;

When combined with inne= r 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 i= t 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 ma= ke 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= .


--ffe53bd37c3d4c8ba370ca23dcc1c6d5--