Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126453 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 86F631A00BC for ; Tue, 18 Feb 2025 17:31:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1739899733; bh=9VnypiHoVYULZlXvPZjP8KBtnDJbCAJ3HipTR2zaPy8=; h=Date:From:To:In-Reply-To:References:Subject:From; b=hID/hjLfO8NwnpKP8uFFvDYU2upHRJbgwII2U/8RO3BBTPJyUsrJYfJqxlHc5LkJX 1+jiEpjzf/xpjmLQcSA3VOBDTwnI/qUaSOwDWEJMopDaXYTypYY25gUdtlW1OCydst +SYj5e6ZMTrvIXvmwYcQt41ivCJFonyb+szCt5vT+vivjqkiv4s9Pn2aSeaEEPnQMA 8rOCs3B+yUlI7rlwxtnfMOZTn0sSBfLt9xsHyBPQWDMY3Vo7ChkNGwOFJW7+6Owci0 1OSmEaoplgLpt++H05ztFRG928tkCADuTzfT0WzFrZVo/uWq2mj0xhv50Tq5Hg5h19 kKoW8ohxeatRg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 25BA2180069 for ; Tue, 18 Feb 2025 17:28:52 +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=-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.0 X-Spam-Virus: No X-Envelope-From: Received: from fout-b1-smtp.messagingengine.com (fout-b1-smtp.messagingengine.com [202.12.124.144]) (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 ; Tue, 18 Feb 2025 17:28:51 +0000 (UTC) Received: from phl-compute-13.internal (phl-compute-13.phl.internal [10.202.2.53]) by mailfout.stl.internal (Postfix) with ESMTP id 6B92211401F3 for ; Tue, 18 Feb 2025 12:31:32 -0500 (EST) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-13.internal (MEProxy); Tue, 18 Feb 2025 12:31:32 -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 :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1739899892; x=1739986292; bh=eO7CIDucNv iAuFu+0eFc4v2Ekix4uBfpHhH29ljvS0A=; b=T4fYYZNHZyQegTaVQBaNn1PrFZ zeb1qd8uBObG4tLHivfRmslqt+Qb8W3qizHTW/TVsZnjJeDRc8KJqZwUvhy+daq1 sUWzm9rxhlucvBSl0BBe7faoq/wDDvRzOB4ExWfNM55eIoZbeZtSwSpPdz9ekygR 7N511FG8BJDZFYmLSSxu6cWxauXLbpTufNV0V44JjhhEeLqk6BWZddd6YSo8phXf SzOPVoAEPRWmAUrUqrKaLu7vGHwmkrkwraq13+K7BD067WPleXxk7b6IQfX+S+kr bcwQHyOynVjxOachODXVG8/y7MJN1g7DpYFXR0yka7jNUPw2pMZfgl23k1mw== 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: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=fm3; t= 1739899892; x=1739986292; bh=eO7CIDucNviAuFu+0eFc4v2Ekix4uBfpHhH 29ljvS0A=; b=In397MI1u4wg61Jk+mNK4IuNlEr3hRZHrzN3fnEGmgsowQUoIGm EzDdFOV5lY3x26aKzdvCOLRg3ntNKVa5nTbU3GsqHmdUrxxIUQO3P6vIO4UFekq8 8VQaltdWlB52rQTkl6TatczNzx18i1exydpgU36yHKzu2NVwwYNsGAtbUmzd8m7B 25UWTq2EW8RLpBY5Z0DPuXDFYBG51GIyjrivqHtJI6j6P/KrJL4i7eF+qbyKAj4z gnI/LWWMjL+j7JTTuV/sbeOS1tq67lsudyIZfv93p9dvbKtD2JrZ3i9fWDFkBV34 GhXtZg3i0TKS6J6R/0Dmm5Ln0o0c1NoQe1A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdeiudelvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepofggff fhvffkjghfufgtsegrtderreertdejnecuhfhrohhmpedftfhosgcunfgrnhguvghrshdf uceorhhosgessghothhtlhgvugdrtghouggvsheqnecuggftrfgrthhtvghrnhepheffud euteehiefgueeutddtueekveeikeevffffgeduvefgveffkeejfffhtdelnecuffhomhgr ihhnpegvgihtvghrnhgrlhhsrdhiohenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehrohgssegsohhtthhlvggurdgtohguvghspdhnsggprhgt phhtthhopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlh hssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id DB7B2780067; Tue, 18 Feb 2025 12:31:31 -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: Tue, 18 Feb 2025 18:31:11 +0100 To: internals@lists.php.net Message-ID: In-Reply-To: <04b901db8225$788d5250$69a7f6f0$@glaive.pro> References: <04b901db8225$788d5250$69a7f6f0$@glaive.pro> Subject: Re: [PHP-DEV] Fully overridable factory methods Content-Type: multipart/alternative; boundary=8276c4444a9b4036908e46d44ccb5650 From: rob@bottled.codes ("Rob Landers") --8276c4444a9b4036908e46d44ccb5650 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Tue, Feb 18, 2025, at 17:52, Juris Evertovskis wrote: > Hi, > =20 > A recent discussion brought up the need for multiple constructors for = the same class. > https://externals.io/message/126428 > =20 > The need was mostly answered by suggestions to use factory methods. > However on an earlier discussion it was discussed how it=E2=80=99s fin= e and even useful for constructor methods to violate the LSP. > https://externals.io/message/121789 > =20 > I=E2=80=99ve felt a similar need on some projects. To have a named fac= tory method that would have an overridable signature. > =20 > Maybe a new keyword like `factory`that would make the method only call= able statically? I=E2=80=99m thinking of something like > =20 > class Triangle extends Polygon > { >=20 > public factory method fromPoints(Point $p1, Point $p2, Point $= p3): static > { >=20 >=20 > // =E2=80=A6 > } > =20 >=20 > public factory method wrap(static|string $triangle): static > { >=20 >=20 > return $triangle instanceof static ? $triangle : static::f= romWkt($triangle); > } > } > =20 > Sure, I=E2=80=99m not saying this API is currently impossible. I can d= o the more narrow checks within the methods and throw from there. But so= metimes it=E2=80=99s useful to precisely describe the acceptable argumen= ts in the method signature. > =20 > What do you think? Would such a feature be useless and/or harmful? > =20 > BR, > Juris Hey Juris: You may be interested in a super-draft RFC that I=E2=80=99ll propose in = the next few days=E2=80=A6 (just fixing a couple of edge cases in the im= plementation) it allows for nested classes and can support what you are = thinking. I will add this use case to the RFC. Adapting your example: class Triangle extends Polygon { public final class fromPoints extends Triangle { public function __construct(Point $p1, Point $p2, Point $p3) {} } public final class wrap extends Triangle { public function __construct(Triangle $triangle) {} } } $triange =3D new Triangle::fromPoints(...$points); I don't know if this is what you are thinking of, but it basically allow= s the expression you are suggesting. If we were to implement your idea t= hough, then nested classes would probably be impossible (and vice-versa). =E2=80=94 Rob --8276c4444a9b4036908e46d44ccb5650 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable


On Tue, Feb 18, 2025, at 17= :52, Juris Evertovskis wrote:
Hi,

 
<= /p>

A recent discussion brought up the need for= multiple constructors for the same class.

https://externals.io= /message/126428

 

The need was mostly answered by suggestions to use = factory methods.

However on an earlier = discussion it was discussed how it=E2=80=99s fine and even useful for co= nstructor methods to violate the LSP.

<= a href=3D"https://externals.io/message/121789">https://externals.io/mess= age/121789

 

I=E2=80=99ve felt a similar need on some projects. To hav= e a named factory method that would have an overridable signature.

 

Mayb= e a new keyword like `factory`that would make the method only callable s= tatically? I=E2=80=99m thinking of something like

 

    cl= ass Triangle extends Polygon

 &nbs= p;  {


  &nb= sp;     public factory method fromPoints(Point $p1, = Point $p2, Point $p3): static
    &nbs= p;   {


 &nb= sp;          // =E2=80=A6

       &nb= sp;}

 


        public fac= tory method wrap(static|string $triangle): static
 &n= bsp;      {


          &= nbsp; return $triangle instanceof static ? $triangle : static::fromWkt($= triangle);

     &nb= sp;  }

    }

 

= Sure, I=E2=80=99m not saying this API is currently impossible. I can do = the more narrow checks within the methods and throw from there. But some= times it=E2=80=99s useful to precisely describe the acceptable arguments= in the method signature.

 

What do you think? Would such a feature be us= eless and/or harmful?

 

BR,

Juris


Hey Juris:

You may be interested in a super-draft RFC that I=E2=80=99ll pro= pose in the next few days=E2=80=A6 (just fixing a couple of edge cases i= n the implementation) it allows for nested classes and can support what = you are thinking. I will add this use case to the RFC.
Adapting your example:

class Tr= iangle extends Polygon {
  public final class fromPoi= nts extends Triangle {
    public function __con= struct(Point $p1, Point $p2, Point $p3) {}
  }

  public final class wrap extends Triangle {=
    public function __construct(Triangle $trian= gle) {}
  }
}

<= div>$triange =3D new Triangle::fromPoints(...$points);
I don't know if this is what you are thinking of, but it bas= ically allows the expression you are suggesting. If we were to implement= your idea though, then nested classes would probably be impossible (and= vice-versa).

=E2=80=94= Rob
--8276c4444a9b4036908e46d44ccb5650--