Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126436 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 887E21A00BC for ; Tue, 18 Feb 2025 09:25:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1739870545; bh=bfzIQIG9o88OWSmOUF/L//FMEhA22U8W24Cj+YxkRpg=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=XfFCFCGfBU6oNBjJvUHLweSmsRu9krQIhgQMQtQyrBB/GvV1vRgInWIWHQlHff0UD XP0HMtvrXyhiERgR3sBEY+SHx0bKtbt42FHQyD+X6DEXTpt8Gd6qwjIZZgKqWQ/v6S U7LLWpOsn1VifcJaXng+29uhu7Of7eKg8jLTn9CY0rzMD4+xzAdtfevPyjfqQHGTZL mJT+2d5LfSK5RxSKD9N57Jtpwnl6X1PemNv4dy+PFX84IUPOLB8vef/icTqtQIK9gH OfkFmWu7PAl2x395+QEc63wdtfzVp04TDTSwx3yLaukLY9UUwJ/sqrKJuc7Ro1g2Kk 0B/cE+QQ6FY3Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 753D5180061 for ; Tue, 18 Feb 2025 09:22:24 +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.5 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, 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-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) (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 09:22:24 +0000 (UTC) Received: by mail-qk1-f182.google.com with SMTP id af79cd13be357-7c081fe0a5eso500785385a.2 for ; Tue, 18 Feb 2025 01:25:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739870705; x=1740475505; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=bfzIQIG9o88OWSmOUF/L//FMEhA22U8W24Cj+YxkRpg=; b=hKhF6cOnhXPjJfSwH4XSt3eq2FciVXQKgRFuWa8yG0v4X4caSA1yCI7mL/w4a16Dd5 dD3JK1k19djw6g2kDaPwLWIBCg7O6hftpcqKCnUIaR9VbLGZn5oIg0X5Uc+bX+xuNlEW nCN8ttDHjTUG8yLgYI1DqbC6wWGJm6SxM+ejC3z0MB7oN9e/8FY6GUC74hG0F2GiSqHs ftcVxuJ6GfqFtC8PG03QfNdMOLJx9EomAI4HlJOMFud7PZhQHRE1zl00RUMRvz1Y6X9x 2IwUMs/+dqCf260pLw8uUmhnLzgKnjbRCqjw+W6qlSGUVoeWPOw+sU9tq7rgjmt3E7RK rvNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739870705; x=1740475505; h=content-transfer-encoding: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=bfzIQIG9o88OWSmOUF/L//FMEhA22U8W24Cj+YxkRpg=; b=cuFuUzlx2unC2f2GC6z7wL8MtBdUsyLDpxoBvLtpzfaXaALntZbCBUMTEjAkFnORVZ 3UtbOiXA1DoQ6QAFKN+5GPdk9XuRMc3+EeSrl7Zw8YB/Vrj7erNrGFeDKMyy9Y/X8PP7 xLxwBxc/pw1YlpmshfOQYf4PCQishVpXmDpQfdXYAl4w/Omjb7n7wfgnnzQI899m5tJZ zoniY7wq3agf5mtvZvyaiV+R3mxKznGVhqEXBWXTby+6nL5Y13BA2AYyywhGnrBeUkCw 8ZmhFn+9vSVnqHWSHp+DNrTwsTBmV/Q0/ho9K5TWZn3M6ZLubBuv42TFAm0wWiwggu5d SiFQ== X-Gm-Message-State: AOJu0YzT9BXEWl07DckMM8mi2XIE8ssyfWX1sWFAvPg1EgCMotaYzAc1 jP2nd+6VfUwTivP3bJ6ZQkKDYUackOikfVMaLCWveTMHEib+VI4XLAvJQ2++VWDYmjUC8OjhpP0 M9fG24O9V+r6NZInn/5mWoyQ6JSs9Gjqn X-Gm-Gg: ASbGncuP38ftrjZ1W15a8cUNksp+5/ytyYmEG9JSZma4SjGtl4IBYulUf/0zDlVenrL HZN7VQXLpZtpa+ed1yLBVfMk1nDUKL5PcBlnjiyWQts0p/0AxxiPF3vDKjMAFaUy2/e4PzUFi X-Google-Smtp-Source: AGHT+IG+FzVV92Ht4D+p97P4kLTYwDAcdacp0stsHTui9QF0qwf9X+dScUaPMjMKSJctrUO9+sAIAG+URhH1CiLfcQw= X-Received: by 2002:a05:620a:2609:b0:7c0:ae2e:6308 with SMTP id af79cd13be357-7c0ae2e64dbmr204198685a.17.1739870704602; Tue, 18 Feb 2025 01:25:04 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 18 Feb 2025 12:24:29 +0300 X-Gm-Features: AWEUYZlPTmqgN6ZOkK_xPYScKkXgQbGcauKGJbkt4jYlz-qvAKI8Ay-B4Tqa-CA Message-ID: Subject: Re: [PHP-DEV] Concept: constructor overloading syntax sugar To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: dev.999.victor@gmail.com (Viktor Khramov) Decision::createFromId($uuid, $comment); Decision::createFromMail($subject, $body); Decision::createFromSome($a, $b, $c); Decision::createFromAnother($a); This doesn't look like constructors. It will be hard to read the class, it will take time to come up with a name, it's not right. I have different colors for functions and magic methods in my IDE, __construct is always in focus. new Decision($uuid, $comment); new Decision($subject, $body); This makes it much easier to quickly understand what is going on. =D0=B2=D1=82, 18 =D1=84=D0=B5=D0=B2=D1=80. 2025=E2=80=AF=D0=B3. =D0=B2 11:0= 3, Rowan Tommins [IMSoP] : > > > > 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 mor= e sense than type-based overloads. For instance, you might have both create= FromJson 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 requ= iring named arguments and despatching based on those names, but I believe i= s actually derived from SmallTalk's multipart method names. > > Delphi is more explicit: a constructor is any class method marked with "c= onstructor" 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 =3D new Decision($a, $b); > > You could write one of these to be clear: > > $decision =3D Decision::createFromId($a, $b); > $decision =3D Decision::createFromMail($a, $b); > > > Even if someone were to come up with a working implementation of overload= ing in PHP's type system, I would probably oppose it, because I think it ma= kes code harder to read and reason about. > > Regards, > Rowan Tommins > [IMSoP]