Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126443 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 A76161A00BC for <internals@lists.php.net>; Tue, 18 Feb 2025 11:45:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1739878996; bh=c+/EQ8JXAsVWIEqjqHsOdsf6x8nUW/5wrivPGqFnRhQ=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=hV8gRabuTuMvLMGjzHKj/JNndLbt8ZWKXY3Ke39/pNAMNMOLV1S4CX0pQUd5QtlAp BhTqX/DLddbI6CsiyfROtmCtiOr/ROQfgzpTcD5aDIP1tNKSMAr7mLbb+Gq5tIqONv 7MqL+kdxEB55k+fl3woHAB9HCkurdC+ZN/Cu4uxsaAJCZa83haQqdS33qUQ0eKiYQz FX4uPopoaKxZ/3fs/75BKHOHmOslwzbAEExnlGXuip5CKwLTgxneTDSDeXSMUY9L+Q 9MDjEnusS1cR1mi7RaeFIf6XGk1j6lmHAumb7Zf7PuNw1646tlYnkzs9kXnBDYwYQQ DTU9XWY9BBW8g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5F15C180048 for <internals@lists.php.net>; Tue, 18 Feb 2025 11:43:15 +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: <kjarli@gmail.com> Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (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 <internals@lists.php.net>; Tue, 18 Feb 2025 11:43:15 +0000 (UTC) Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-5dee1626093so7115840a12.1 for <internals@lists.php.net>; Tue, 18 Feb 2025 03:45:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739879155; x=1740483955; 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=c+/EQ8JXAsVWIEqjqHsOdsf6x8nUW/5wrivPGqFnRhQ=; b=MeQ9LDQTmkcEEbggwlrZY2O5DJ/qs97gbWKOfnD1PveiCORC7UlO/6YTxGbmprberN It/Rm7N+vKQY3toF6p8wiyRdga+hYcDE9R7xeCBUVgns57/NCX6Iy6vRKhJjewhp729j ulXF0hzG3N1Ugx5Ciiv4/pNNah1LLmrUA65Bx498+KdjIWik0fId/nAq8AXgLI6xZIyM KoTjLnelr/IBbDR3G38go8xbNsV6a+TGT2AeyO7JDKVHduEH7gCtSUBe8Al1wu9Te0xc tX4ImrMWYV00Rb8Xzyib9ULby6RHicTX8RkA6Hmy2KZMKJPzgpUYnjc6e8U0q6zBQFAI nsHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739879155; x=1740483955; 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=c+/EQ8JXAsVWIEqjqHsOdsf6x8nUW/5wrivPGqFnRhQ=; b=I7KqTLhcd4mjmCCrU8tE4yFSU724Z5O7Yi5wlZTFduK+AjB5I9/rmQExmGdpsw2bPX LQxtlEsn9Kz8tKSpDz9UoCpu8GZiDPkvSQR9UxN/g5Lmk618zKh83C2yNMYsc/Kz/N7/ aM+qu0j9GhNW/AT8Akd+XTeI3d4yPk8G5TAJ9hh+IS3sZUHM7lsZ+aElJhNjIX1vhZh5 D3on9UShmz7xggeHgVsRT7S2SWqJnxptpSYK+QsodLhDbvRNYPU+SUAmxx3F10hjsuSo xdXTNrPRsu+G+5hC+RGceyc63KJ+kAFXQid7uCtlQYgnw5/ry+PdbFFuwPq0jOA8ON61 mymA== X-Forwarded-Encrypted: i=1; AJvYcCV2MXAYjg1X68TTuz8ucsVzJNFvTldzHFhlDuwjXdbKJi1sTFdFrNSNuSWUK1TZiMD61APXl1LU0uA=@lists.php.net X-Gm-Message-State: AOJu0YyaBiPZ3FcXWyEZ1eFsmnM4EUP7EqphzM+GIdk9qzfZbtlsSecn RaAuL/WIMFonlmH91QyvdGMrMsyxnoNLit/nduqDauaIJ7lqW5V0XRBMT02MPnzLN2+6JfbpJiP MXV6Sna7Q/Yf0ueuYkFt1vnVMZBBnJCb6 X-Gm-Gg: ASbGncvDaaG1H9nh0QG/jNewCV15Ae18K5LHuT4/IJlTPKnM46rxLi1LrDvbcoN9JPq x9KuKhIRvc1DDwZ72Mhe08BYA7NDnIA1X8ItjZiLbRlnY5p86noQYQ2NLcXUlhbgtaaz4McHdQR rzZSHJfbmt262Xz8+knkDaaG8FpFg= X-Google-Smtp-Source: AGHT+IEkwABlDKBr/mo8TOSxIB5d8gQGp1PG8xzAwMa8RcVGMp+Z1hwbNG4JKtJfE3n16tPR8XoseGENIYICIftFo6k= X-Received: by 2002:a17:907:784b:b0:ab7:d66f:c872 with SMTP id a640c23a62f3a-abb70c0247amr1182281166b.19.1739879154606; Tue, 18 Feb 2025 03:45:54 -0800 (PST) Precedence: bulk list-help: <mailto:internals+help@lists.php.net list-unsubscribe: <mailto:internals+unsubscribe@lists.php.net> list-post: <mailto:internals@lists.php.net> List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <CAA0W4qKeJ-KXGmQFCsF6wMN746cbFhUF-D71zxdNtX2xrU_tCg@mail.gmail.com> <BB121A3F-789F-4FEA-B032-ABC455E1E56C@rwec.co.uk> <CAA0W4qKgYvs1_ha8K+6HAvfCL8r-Zkdf3jfrmz1ioATEUGwPWQ@mail.gmail.com> In-Reply-To: <CAA0W4qKgYvs1_ha8K+6HAvfCL8r-Zkdf3jfrmz1ioATEUGwPWQ@mail.gmail.com> Date: Tue, 18 Feb 2025 12:45:27 +0100 X-Gm-Features: AWEUYZlx-T0h0KxQ-DGr9qhC013_sJTBYrc7T_JpewWbfD85u47X0y4ZArVD7EE Message-ID: <CANS-=pcmbQJOARv=4hN9o1r2q6hL=k2vR=vbPUyko8PYB3=5=g@mail.gmail.com> Subject: Re: [PHP-DEV] Concept: constructor overloading syntax sugar To: Viktor Khramov <dev.999.victor@gmail.com> Cc: "Rowan Tommins [IMSoP]" <imsop.php@rwec.co.uk>, internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000f669d7062e692e38" From: kjarli@gmail.com (Lynn) --000000000000f669d7062e692e38 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Feb 18, 2025 at 10:27=E2=80=AFAM Viktor Khramov <dev.999.victor@gma= il.com> wrote: > 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= :03, Rowan Tommins [IMSoP] <imsop.php@rwec.co.uk > >: > > > > > > > > On 17 February 2025 14:39:42 GMT, Viktor Khramov < > dev.999.victor@gmail.com> 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 =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 > overloading in PHP's type system, I would probably oppose it, because I > think it makes code harder to read and reason about. > > > > Regards, > > Rowan Tommins > > [IMSoP] > My only "issue" with static constructors is that it's unclear what you can do, especially if you mix constructors with static constructors. Typing "new TheClass" in PhpStorm for example won't show if the constructor is protected/private based on the scope, and won't show all possible constructors. On the other hand if type `TheClass::` PhpStorm won't show you the normal constructor and none at all if you don't have any static functions. I don't know if there's a good solution to this problem though. Could look into: - `[#Constructor]` attributes - allowing "constructor" instead of "function" when defining the function - these possibly have more benefits such as with-ers and non-static factory methods on other objects While I'd love to have a better way to quickly know what functions are constructors, I don't know if this is something that should be solved in the language. --000000000000f669d7062e692e38 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div dir=3D"ltr"><br></div><br><div class=3D"gmail_quote g= mail_quote_container"><div dir=3D"ltr" class=3D"gmail_attr">On Tue, Feb 18,= 2025 at 10:27=E2=80=AFAM Viktor Khramov <<a href=3D"mailto:dev.999.vict= or@gmail.com">dev.999.victor@gmail.com</a>> wrote:<br></div><blockquote = class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px sol= id rgb(204,204,204);padding-left:1ex">Decision::createFromId($uuid, $commen= t);<br> Decision::createFromMail($subject, $body);<br> Decision::createFromSome($a, $b, $c);<br> Decision::createFromAnother($a);<br> <br> This doesn't look like constructors. It will be hard to read the<br> class, it will take time to come up with a name, it's not right. I<br> have different colors for functions and magic methods in my IDE,<br> __construct is always in focus.<br> <br> new Decision($uuid, $comment);<br> new Decision($subject, $body);<br> <br> This makes it much easier to quickly understand what is going on.<br> <br> =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] <<a href=3D"mailto:imsop.php@rwec.co.uk" target= =3D"_blank">imsop.php@rwec.co.uk</a>>:<br> ><br> ><br> ><br> > On 17 February 2025 14:39:42 GMT, Viktor Khramov <<a href=3D"mailto= :dev.999.victor@gmail.com" target=3D"_blank">dev.999.victor@gmail.com</a>&g= t; wrote:<br> > >Hi!<br> > ><br> > >The point is here:<br> > ><a href=3D"https://gist.github.com/vhood/665418835e65be26d5a818fde= d92ab75" rel=3D"noreferrer" target=3D"_blank">https://gist.github.com/vhood= /665418835e65be26d5a818fded92ab75</a><br> ><br> ><br> > >Static functions look awful and break the object's API.<br> ><br> > Personally, I think quite the opposite: named constructors make a lot = more sense than type-based overloads. For instance, you might have both cre= ateFromJson and createFromYaml; their inputs are both going to be "str= ing" as far as the type system is concerned, so overloading would be u= seless.<br> ><br> > Swift gets around this using "argument labels" which is sort= of like requiring named arguments and despatching based on those names, bu= t I believe is actually derived from SmallTalk's multipart method names= .<br> ><br> > 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.<br> ><br> > In your example, instead of this meaning different things based on wha= t $a happens to hold:<br> ><br> > $decision =3D new Decision($a, $b);<br> ><br> > You could write one of these to be clear:<br> ><br> > $decision =3D Decision::createFromId($a, $b);<br> > $decision =3D Decision::createFromMail($a, $b);<br> ><br> ><br> > Even if someone were to come up with a working implementation of overl= oading in PHP's type system, I would probably oppose it, because I thin= k it makes code harder to read and reason about.<br> ><br> > Regards,<br> > Rowan Tommins<br> > [IMSoP]<br></blockquote><div><br></div><div>My only "issue" = with static constructors is that it's unclear what you can do, especial= ly if you mix constructors with static constructors. Typing "new TheCl= ass" in PhpStorm for example won't show if the constructor is prot= ected/private based on the scope, and won't show all possible construct= ors. On the other hand if type `TheClass::` PhpStorm won't show you the= normal constructor and none at all if you don't have any static functi= ons. I don't know if there's a good solution to this problem though= . Could look into:</div><div>=C2=A0- `[#Constructor]` attributes<br>=C2=A0-= allowing "constructor" instead of "function" when defi= ning the function</div><div>=C2=A0- these possibly have more benefits such = as with-ers and non-static factory methods on other objects</div><div><br><= /div><div>While I'd love to have a better way to quickly know what func= tions are constructors, I don't know if this is something that should b= e solved in the language.</div></div></div> --000000000000f669d7062e692e38--