Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126452 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 DE68D1A00BC for ; Tue, 18 Feb 2025 16:52:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1739897385; bh=INfQNb4KGTWgCk3DOmUCGBHcWJtsjoznQSxeusOZoBU=; h=From:To:Subject:Date:From; b=lhLf+Sz79fYsIbMUXhhRhDgVZ1gD0f4kQlXZBw2fj28bb6eTFjs/c4aIP53aa0fFw jQzA6XL6W7St6RuECsfvP7QN2saWfJxxWvv5B8wTj1FQqSG0FhlQiQu6vud5rdNSo8 Cbkp7mk7FWqPN9GfguUZNUQLJR6wClDHp8dC7uANMcx8ecBddscAG87BURHe9DKKbi sGZ7dtqvHsEztwK/VYURZ6Vx0i3hLqWnklYkh6raaTcOAxfZlBLiLcgHUz8RPmBoCc nFBZIwiNwo0aMke9LxspH4T7AdinZAIEupYhPf7AuOIuOi0udUUhT2cgbxi9ovdyWc Y7ZJaiE9TB9HA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 646BC180071 for ; Tue, 18 Feb 2025 16:49:44 +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=0.6 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_50, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,T_PDS_PRO_TLD autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from black.elm.relay.mailchannels.net (black.elm.relay.mailchannels.net [23.83.212.19]) (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 16:49:43 +0000 (UTC) X-Sender-Id: yszpovajlk|x-authuser|juris@glaive.pro Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id AE46E8379E for ; Tue, 18 Feb 2025 16:52:23 +0000 (UTC) Received: from server42.areait.lv (100-97-164-218.trex-nlb.outbound.svc.cluster.local [100.97.164.218]) (Authenticated sender: yszpovajlk) by relay.mailchannels.net (Postfix) with ESMTPA id 0754D81ACC for ; Tue, 18 Feb 2025 16:52:21 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1739897542; a=rsa-sha256; cv=none; b=CNeFKkjDOmb5FQY5g6A/9f4+UjbeQlELnS1cDMb5mlu3ePmIjL//02uu6lZnp64MQb+duO oCU2M6oMpCmBfaQ4hZIOP1tYPkW1c15c/ivxb8i+HyAWrtZ29gCRUkRm2tb1tudsA6f0Bd aOsxC5GwWV+YSD+f3S3H0LDFZJ0RJ8/hUZ9556WKUaQPdGAK4yoA4rTIy9wx4CCl5WRDbo K7CB2o1TTv7egCMPbDSYtchfJHpl+jOR8END1BeSGIQkDaAxAW1BBfDH5v3+kbmq8SKWW5 opyp9aMkPInzunXsQbxKYMXJlrQOOAi/oRIynz0Kd8u9dbniT8pr9OzHl1RzVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1739897542; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: dkim-signature; bh=aEgbKvG2mTe9K8AO0OY/N+FMMV10sVIE9TzkHNDlE/A=; b=xxy7LeZ1ITiwbRPJtZZ6ch0I8CR4gH8xihA7HQ0Qqov+yO2orxi5DLUHmrxT8bDwTvmXka O3WEJUGrbRIfMiQHpHUdq8NdVewuLV6kpKk4IAqgurdLNJhYdnkZtbZ/lZTSZBlqw4pekX 9mfFp/xldPLDoLHptxGogthWqQoLDyBUDNpmfyHkpEwklurkbTHd852HJQz2F3TszBjBzc mfLo25h7uTTQOSM5SdBOocUKQPuBbVyVxR1k0wRwpBobVXB6EtXgFTPw2If0QfiSNWjt1H /6RrA0uWKRCN9HWjSg1RFK6wATAKTdiVkzXdMwiOKzkt/IZRjuA9eyw+CkqChg== ARC-Authentication-Results: i=1; rspamd-6d7cc6b78d-qbjwd; auth=pass smtp.auth=yszpovajlk smtp.mailfrom=juris@glaive.pro X-Sender-Id: yszpovajlk|x-authuser|juris@glaive.pro X-MC-Relay: Neutral X-MailChannels-SenderId: yszpovajlk|x-authuser|juris@glaive.pro X-MailChannels-Auth-Id: yszpovajlk X-Hysterical-Blushing: 54908f376efbe207_1739897542537_2643496374 X-MC-Loop-Signature: 1739897542537:2251387040 X-MC-Ingress-Time: 1739897542537 Received: from server42.areait.lv (server42.areait.lv [212.7.207.88]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.97.164.218 (trex/7.0.2); Tue, 18 Feb 2025 16:52:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=glaive.pro; s=default; h=Content-Type:MIME-Version:Message-ID:Date:Subject:To:From:Sender :Reply-To:Cc:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=F4VWT0zmOsPZ3SxuJqfTXyejtZfQ1KRMAhVlyn/kRCg=; b=cnwkSL1CeeAVlYQeM4hOGrFkfU getT6cSoQGUF5c8Ou2TXl5WhUXWAXYsU+4hJ7twIE85+E6ni/wvAe0CWYgpCF70HzJ+EnArkqELRm 79PTOgKGNr7mE5JNGDLeTBJUpbBODiFPrhXelnFCdHsjszystKA3f2jwhMDegWxnndMHxMQ9vfVTd GGCV+c3+n8eYDD5Dst4C/dccFpfxOy3IkN5Ugkx9Kgzz+xEH0y8cMrzR9q66Llnink0wKQT0u8fo5 qYFhp/I0xKvx+H988csOO3YkIpV8G6Qsx6Kb31wX6IkJL1Awfg1Rz69dL+Z1xbw+BeHSFCdf+4JZf Xoyd3ffQ==; Received: from [77.93.29.116] (port=62637 helo=LAPTOPHKIOPCGI) by server42.areait.lv with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1tkQpX-006Iy6-0l for internals@lists.php.net; Tue, 18 Feb 2025 18:52:20 +0200 To: "'php internals'" Subject: [PHP-DEV] Fully overridable factory methods Date: Tue, 18 Feb 2025 18:52:17 +0200 Message-ID: <04b901db8225$788d5250$69a7f6f0$@glaive.pro> Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_04BA_01DB8236.3C170CB0" X-Mailer: Microsoft Outlook 16.0 Thread-Index: AduCJUqIqf3nWIgfT2aCk5ifrTSKOQ== Content-Language: lv X-AuthUser: juris@glaive.pro From: juris@glaive.pro ("Juris Evertovskis") This is a multipart message in MIME format. ------=_NextPart_000_04BA_01DB8236.3C170CB0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hi, 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's fine and even useful for constructor methods to violate the LSP. https://externals.io/message/121789 I've felt a similar need on some projects. To have a named factory method that would have an overridable signature. Maybe a new keyword like `factory`that would make the method only callable statically? I'm thinking of something like class Triangle extends Polygon { public factory method fromPoints(Point $p1, Point $p2, Point $p3): static { // . } public factory method wrap(static|string $triangle): static { return $triangle instanceof static ? $triangle : static::fromWkt($triangle); } } Sure, I'm not saying this API is currently impossible. I can do the more narrow checks within the methods and throw from there. But sometimes it's useful to precisely describe the acceptable arguments in the method signature. What do you think? Would such a feature be useless and/or harmful? BR, Juris ------=_NextPart_000_04BA_01DB8236.3C170CB0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Hi,

 

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’s fine and even useful = for constructor methods to violate the LSP.

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

 

I’ve felt a similar need on some projects. To = have a named factory method that would have an overridable = signature.

 

Maybe a new keyword like `factory`that would make the = method only callable statically? I’m thinking of something = like

 

    class Triangle extends = Polygon

    = {

        public = factory method fromPoints(Point $p1, Point $p2, Point $p3): = static
        {

         &= nbsp;  // …

    =     }

 

        public = factory method wrap(static|string $triangle): = static
        {

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

    =     }

    }

 

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

 

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

 

BR,

Juris

------=_NextPart_000_04BA_01DB8236.3C170CB0--