Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126921 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 D8A3D1A00BC for ; Mon, 24 Mar 2025 09:20:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1742807874; bh=TAdvUvF8vaGYzgAXqWRVS4SFt/P5CI9Kt9amxUlBkqQ=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=jnB+NKZVDxk2iKA/1poePLOqOPcrJkOgCAT3jSRhdbXSi2E95NIp3BJhWvSJz/NKB yfZ79iZmpBAhSWkPdlLdhiRcrjrx3eIToS3mz+M0vVS3d76+6zmPrL1wuSexyQqHBj zboj8K0SM2+/Ck3P/2HQBgzOXnsAOzOwNXnuWTPuokOSGQKFEhc31sik532hi7jazK zIMyJL2RYLz9F3/lkpBQjWEXXN2wLkaS+6UHpDsSDLuRGiOgS3xG6c/MKd/LmL2maH 1ykFmG6/etJzWOd7T4QAgM8Sxs4TbHAynPCBo7szDBFrai5LFPzvOA13ezd1HqcjPE rDdEDOe4ASwDA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2B9341804BF for ; Mon, 24 Mar 2025 09:17:53 +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.0 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (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, 24 Mar 2025 09:17:52 +0000 (UTC) Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-700b17551cdso36624197b3.0 for ; Mon, 24 Mar 2025 02:20:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742808022; x=1743412822; 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=3bGYkTrnCHTVFMsMn7vct9hP49mLDfKRgqRrgIqr/yU=; b=iN3i9++fazIHeskNQQRVEFbCvzDYvha3mHOHWnR4mURvI5Sw3ZQROcWjJE+bDEm2+j G1+6lLNR3Ge6h0DVPaJe74DA2c84Die8o1Qtc3iEs6lIxoove3Waljw/jjyEhBjRVf4a kPPw8B4t8BGMoipJgdwaM5DGFLcNApZ735D5B3aRG0/kV5mLm5A7AxYc8Il67ssZSNWe Kl/URewBXW4yd5ww5LuqpDPiUS4OQ+0z2cAuaEX9xb68W1uTaoKtAdANfAqGdxmuNO93 qkw+G1HLKWXkW035VGg0AApW5yYxMlRsprtex+R7iG6HrMv0nR1HGIXS+JQAmAjDTTOI qzfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742808022; x=1743412822; 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=3bGYkTrnCHTVFMsMn7vct9hP49mLDfKRgqRrgIqr/yU=; b=NdrXnVN2/62si/Oaamcc6dnFZ+Ff8Xpq0JZGJVn/XwOTkjE1qgzMeKq/KLc9jGyjoU vZ57fQf3hubwytTNY/VvZi+DW44q9xCW2++aWC/FWjQkyyNixVmn+Lh6t+dvQ6sUR/SR Vj9mnOgY/z7R9zY4S7mF6punf2Qmdu6VQ/v1tNVPJI5hVU/97AybyZD/ZAn7ty2pf88G wdqk05Ig9DKv3R9fkyElCWpGytnumXxxch7m/fEa6j/G0xwEooz230AcwOqf5rMD+7rz Y8neoHPYdwSFuRX2yd/UjDzFL0ZwSJQh1RDifvSjqEGsq4xe6sO/gafXjT0/8Ar+x0VA b0aQ== X-Gm-Message-State: AOJu0Yzqx3qRi6JRbg1SCD+yR+zTB9UEE+zB5ss4nO0sH17x/4CKOroD g2QSI8mn0ycnBjbTljFQ00uYcuTm4qG/s9UrmGLjm5eQq3RnjdrapoiQ6nhvePwipHy/PMFevg1 PchwJxqMOP6Wv7cJSXdoCZWOMiP5YPfwDRlg= X-Gm-Gg: ASbGncsANFax7g9Nf10n1NTD1085BbNYyA0H8SdngqOSBlW7EBNPMqgiKzjxSTBWZWy Ie5RkBE4k09gPB/a2gJhT9y1TGVOTU2lDgl8HL1mZIYD5bJuSd0q0GZYUQOuwKWXH+rPVlfepH5 1TTO009WK+ca6ztpVgZyuysEN+/jaYTmaZUdZ5 X-Google-Smtp-Source: AGHT+IHJeYTtYIJaNkfypGgObFEmsl72yI/TkhMxFrdXhyCZ3GbB25EUObjr/OeVeKMyu9AfUEe/dYiZJeoiBB+nOGU= X-Received: by 2002:a05:690c:7444:b0:700:a690:12d3 with SMTP id 00721157ae682-700bad02382mr157271807b3.31.1742808021537; Mon, 24 Mar 2025 02:20:21 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <2935d0e2-ddc4-447c-ab37-c9b7337b8b60@app.fastmail.com> <0db0cce1-c2da-4298-a7a0-c9c9c82ab498@app.fastmail.com> In-Reply-To: <0db0cce1-c2da-4298-a7a0-c9c9c82ab498@app.fastmail.com> Date: Mon, 24 Mar 2025 11:20:03 +0200 X-Gm-Features: AQ5f1JpluVR4u0C0H8DAapUqTscwozOD83kc4_7HOTkAp-p1yuUTcFLkwWsbVW4 Message-ID: Subject: Re: [PHP-DEV] Re: RFC: short and inner classes To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000091fa00631131dec" From: drealecs@gmail.com (=?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?=) --000000000000091fa00631131dec Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Mar 23, 2025 at 5:20=E2=80=AFPM Larry Garfield wrote: > > So, how would nested classes compare to fileprivate, in terms of ability > to solve the problem space? As I understand it, the goal is: > > 1. Classes that can be instantiated only by the class that uses them. > 2. But can be returned from that class to a caller and reused as > appropriate. > I think the one other difference is that nested classes can access private variables or their outer classes. Example: ``` class Polygon { private function __construct(private array $points) {} public function getPoints(): array { return $this->points; } public class Builder { private array $points =3D []; public function addPoint(Points $point): self { $this->points[] =3D $point; return $this; } public function build(): Polygon { if (count($this->points) < 3) { throw new InvalidArgumentException('A polygon must have at least 3 points'); } return new Polygon($this->points); } } } ``` And it would be used like this: ``` $polygon =3D new Polygon::Builder() ->addPoint($point1) ->addPoint($point1) ->addPoint($point1) ->build(); ``` With no way to create a Polygon otherwise, due to the private constructor. -- Alex --000000000000091fa00631131dec Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Sun, Mar 23, 202= 5 at 5:20=E2=80=AFPM Larry Garfield <larry@garfieldtech.com> wrote:

So, how would nested classes compare to fileprivate, in terms of ability to= solve the problem space?=C2=A0 As I understand it, the goal is:

1. Classes that can be instantiated only by the class that uses them.
2. But can be returned from that class to a caller and reused as appropriat= e.
=C2=A0=C2=A0
=C2=A0
I think the one other = difference is that nested classes can access private variables or their out= er classes.
Example:
```
cl= ass Polygon {
=C2=A0 =C2=A0 private function __construct(private array $= points) {}
=C2=A0 =C2=A0 public function getPoints(): array {
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 return $this->points;
=C2=A0 =C2=A0 }
=C2=A0 = =C2=A0 public class Builder {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 private array = $points =3D [];
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public function addPoint(Poi= nts $point): self {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 $this->= points[] =3D $point;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return $t= his;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public= function build(): Polygon {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 i= f (count($this->points) < 3) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 throw new InvalidArgumentException('A polygon must= have at least 3 points');
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return new Polygon($this-&g= t;points);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 }
}
<= /div>
```
And it would be used like this:
```
=
$polygon =3D new=C2=A0Polygon::Builder()
=C2=A0 =C2=A0 ->addPoint($point1)<= /font>
=C2=A0 =C2=A0 ->addPoint($poin= t1)
=C2=A0 =C2=A0 ->addPoint($= point1)
=C2=A0 =C2=A0 ->build();
```
With n= o way to create a Polygon=C2=A0otherwise, due to the private constructor.

-- Alex
--000000000000091fa00631131dec--