Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126458 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 E83A31A00BC for ; Tue, 18 Feb 2025 20:02:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1739908797; bh=VsnqAlDLVTokrBf9K6B/l+g+IaBeOPabuPzkcEErwco=; h=Date:From:To:In-Reply-To:References:Subject:From; b=U69Whqy980BjUSef3aPKL+EuatQSSV7X2+TcBGhx2Syh+VOHiMqpDhbr2EOvxl1HU 1rdC4KBXPbQoD8iSgbm9LpGBWqDqDdOa/lUXppV6eN+2TdWYZpMcgulcqdPp9sSLNv qa76G9ftdBBNLTHylVSy53dEbYVBRIblLopxM18u8T73qVJk8dN1QfSN/n61ycNqoP vjbMDh4qhdnsN1XggukfGvAUafsBGkFEOpcuGF3w2CJCN2zIlDsZzamVP4WtoTESpM Xh6QtsOt5fnnm3maIojwks48oTkHGmiJtFpfA1wsceAho3MVoYrbmnhZ5AJ8m9CZOa I/fovTctcQRaw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 492D51801E7 for ; Tue, 18 Feb 2025 19:59:56 +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,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b2-smtp.messagingengine.com (fhigh-b2-smtp.messagingengine.com [202.12.124.153]) (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 19:59:56 +0000 (UTC) Received: from phl-compute-10.internal (phl-compute-10.phl.internal [10.202.2.50]) by mailfhigh.stl.internal (Postfix) with ESMTP id 6AE312540169 for ; Tue, 18 Feb 2025 15:02:36 -0500 (EST) Received: from phl-imap-06 ([10.202.2.83]) by phl-compute-10.internal (MEProxy); Tue, 18 Feb 2025 15:02:36 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding: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=fm3; t=1739908956; x=1739995356; bh=RgHPegqDKHFqrYZwijEWc U3G8VLL18Y20OwZs3sHVDQ=; b=momUqJkL+gIheyi5TQvyOzClkwx2iOx3ueYYQ lQytCXPw+u2XxTXpKskDFQA5a7NrROIav1TcinJ+GBO19Baoj5uKMlmDqk0C1i14 vjBfulXTp7iFCCc7EKtS5fh/CFfitLjL86iVuz1Znx5XewyMyzoBHAVgNkNxT0QM K0J0HLU9FNPGQAwGOFVSUBTYwaQ8PZQX4iozRVxAnot6PHTdYBhSqmjpq6CiXIBE 3+lSKz/2+8FeNwQaAgt+hAlYz8sp1vheOvEnjIkq6sm/3p7044VDYdTutwo3f3g4 eRZXsxZtl9fkJEhP7VGfhxoiWLhS+sWW4yYOy03DCRfiZ/ZcA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding: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=1739908956; x=1739995356; bh=R gHPegqDKHFqrYZwijEWcU3G8VLL18Y20OwZs3sHVDQ=; b=Hw4DIpIE8Vz0bET0j 1Va7YhlW+H0eT2CO9yQODoyjHohPnfnnL/phQ3wm/gS/FxKJt7pYWypWny3f5mFi 10QE4BG+tCJzhqSf5iJWNs+I7QhBHKjj9jmL1xrpQ8+GdzZHni5aTRT0ti+14Wba Y+wVB/boGRqXYD7Wyyl20z8EBkDoByh7oYFXwn6l9AJcULFaBLNeQoWst4VPNKbf nMMyEhg91VfrYQ+jYiH2lNxrEylLqHfJKTnBl3lIfQt3IumT9PtS/aRTs6UdY/oV pasw5TvPqM/bp1UWiF+k/8MyggwvLspPP0EVkKav9LUSqyrbmkC7gjOMpShBHmGs G980w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdeivddvfecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefoggffhffvkfgjfhfutgfgsehtjeertdertddt necuhfhrohhmpedfnfgrrhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfh hivghlughtvggthhdrtghomheqnecuggftrfgrthhtvghrnhepueehjeduleetleeiheel leelkedvtdfgtedtgefhgfeghedvjeegjeefffeuhfdvnecuffhomhgrihhnpehgihhthh husgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhr ohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomhdpnhgspghrtghpthhtoh epuddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhi shhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id B512729C006F; Tue, 18 Feb 2025 15:02:35 -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 14:01:42 -0600 To: "php internals" Message-ID: <1809090f-fd9b-4e90-916e-29970ee2780a@app.fastmail.com> In-Reply-To: References: Subject: Re: [PHP-DEV] Concept: constructor overloading syntax sugar Content-Type: text/plain Content-Transfer-Encoding: 7bit From: larry@garfieldtech.com ("Larry Garfield") On Tue, Feb 18, 2025, at 2:00 AM, Rowan Tommins [IMSoP] wrote: > On 17 February 2025 14:39:42 GMT, Viktor Khramov > wrote: >>Hi! >> >>The point is here: >>https://gist.github.com/vhood/665418835e65be26d5a818fded92ab75 > > >>Static functions look awful and break the object's API. > > Personally, I think quite the opposite: named constructors make a lot > more sense than type-based overloads. For instance, you might have both > createFromJson and createFromYaml; their inputs are both going to be > "string" as far as the type system is concerned, so overloading would > be useless. > > Swift gets around this using "argument labels" which is sort of like > requiring named arguments and despatching based on those names, but I > believe is actually derived from SmallTalk's multipart method names. > > Delphi is more explicit: a constructor is any class method marked with > "constructor" in its declaration, and the name "Create" is just a > convention. It does also support overloading on types, so other names > are rarely used, which is a shame. > > In your example, instead of this meaning different things based on what > $a happens to hold: > > $decision = new Decision($a, $b); > > You could write one of these to be clear: > > $decision = Decision::createFromId($a, $b); > $decision = Decision::createFromMail($a, $b); > > > Even if someone were to come up with a working implementation of > overloading in PHP's type system, I would probably oppose it, because I > think it makes code harder to read and reason about. I largely agree. I am open to method overloading as a general feature (which is probably the minority view around here), as I do see places it would be helpful. (Enum methods, operator overloads if those ever happen, etc.) But constructors feel like the least applicable place for them, and named static methods works just fine in my experience. --Larry Garfield