Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126099 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 C923A1A00BD for <internals@lists.php.net>; Wed, 4 Dec 2024 16:35:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1733329930; bh=RVZXkzQcCtFiuDXaz5paNidD13jIKyGpNu7Dg+wvJaE=; h=From:Date:Subject:To:From; b=gIktVPgzKxWmp00O6hvkmfTgR+4dwJL+UtA7atLwfVIOnYJfrcvJEnt3DmnYvBuJo +sbY/XJCcAdEqg+ElTT/ZnbiyuDS9V1xXbf5oAkyl4a+qFtv5mvueTriXP8Bbo8NM7 hBTkVqDpI/2lCebMlb4uvafphRw7RKX17ON9LM6oq91d/YVLFTLeu+PCPuJwrJ6n84 aw6LBLFQgONpgZ0TR8+5JTyUZOk2Q47aiOCdmm9BvVf8TA6JKdUGO/Z2FSv+GcBqJg LUhDfoNknkK2Ocw6546ds+gZE7rvz77/RGzdnmuZyyUdbJmASpKJGBERVZlSZc9vNH ZwEjMK6dNTXqw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1AD5518003F for <internals@lists.php.net>; Wed, 4 Dec 2024 16:32:10 +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=BAYES_50,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: <taras.chr@gmail.com> Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (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>; Wed, 4 Dec 2024 16:32:09 +0000 (UTC) Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-385dece873cso170f8f.0 for <internals@lists.php.net>; Wed, 04 Dec 2024 08:35:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733330116; x=1733934916; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=XyRCVqFEalcD7a5GM5/l+py55Ar6JW/SAEs2pkWhLDc=; b=b7gsxucm9WdFPKJ72njFUc5VaCloVmlXXJZb2BwidGw48Nc6MLMoVUqRaslMjdaD51 xwQ1ekk09c6qVek256cWmAPjCRvmuP6RdUib7/hRmXX3KhowtS35Q4QjhZSMnG77vs5o xzil1nSxBsncMs+cVjpLbk7iD+Ss62DCFRTCRp3jS6eLmRxIChJ6JhcT2t36Mupm8Rmf XXbJ0xndT6H8im2DSuOCZeBE89omOB8Jafi8de8/N4eOO5An2o+9dbubDP9H1gN9i5vZ CxPaF8CjjG1PAbl3vZJaU3RkGtaS4NLJSWrE4kyMFl73j59K9HY1qucKF0QVYBr47rS1 cB7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733330116; x=1733934916; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=XyRCVqFEalcD7a5GM5/l+py55Ar6JW/SAEs2pkWhLDc=; b=W4VCQ7sDX3gqOFVGIPO9rd09O5TJ6BVwUA/x7ikUW3Odo3BzzGhdej8zBIooWsIttB /CL3H0El5RvPmJ1cwXUjXJZOT5MX0qMlhG6RcDHbtpvrdmC68FX+4G5+6+2by3rS2XRp 3qWVv7D+lBXTG8NCtSZ1GvoxX7cK3xZ0H070SlL8m/LKQ3qdXa53BV+BtOfUt7PoM5D8 dO7AFzuGwWIY4zYayg7xoL89cUUCbZm9XN4ruKaeWBDF5izsiGWgeSaT0SJIg8mbfXyy BILFRdhWITwyiIGRDix/FjNaYf6s/kFCJncreqgvwUJj+bSovaBcYouURO0TkSZZuK31 Pubw== X-Gm-Message-State: AOJu0YxOaz20cjpulMwKpL/hSUteAYn5jUfOPTHMqqFvgREDRRpkQZSR 5QDTJDuRuVI8SPYm1NoNoBSEsm7kT1wNoYzF7d26cVXnOmZdDWEgytNaA9jC5mkjXPa1CrEz+B0 NRYmzo3yLn1BXIhXGbmEjc/vJDXlI304C X-Gm-Gg: ASbGncvlFwtcC8448MpRMAE8hkHx5a2Tbl2sbjOE5uegZtZQ85m3Z5TJCQ8eFo8TPR2 FnvDRNjrcNEycuEWT+xZH17K4zO791w== X-Google-Smtp-Source: AGHT+IFJES2jpVGCFwrTBxaYLD4k78RhgeYO+KLYrL/61CbMjloCRMeitGjXZuhZf774VvkVhxR/u9+lMCPX/6TMF8Y= X-Received: by 2002:a05:6000:42c1:b0:385:dfab:1643 with SMTP id ffacd0b85a97d-38607ad2524mr3098671f8f.27.1733330115965; Wed, 04 Dec 2024 08:35:15 -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 Date: Wed, 4 Dec 2024 18:35:04 +0200 Message-ID: <CAOrgP8v0AGF10JGep-YaqC+w73gAjewUwjRN8-AbxmO3wG-iJA@mail.gmail.com> Subject: [PHP-DEV] Multiple property assigning without constructor To: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000d741520628745d54" From: taras.chr@gmail.com (Taras Chornyi) --000000000000d741520628745d54 Content-Type: text/plain; charset="UTF-8" Hi Internals, I would like to discuss the possibility of assigning multiple public properties at once during the class initialization. Currently, with constructor property promotion we've already achieved the ability to skip a lot of boilerplate code. With property accessors (aka hooks) we could rid of getters and setters. But often classes need constructors only to set properties. In such cases, constructors become redundant. Here are a few examples: ``` class Person { public string $firstName; public int $age; } $person = new Person { firstName: "John", age: 42 }; ``` The initialization becomes "struct-like" but it should not affect the current state and in general it makes code work like this: ``` $person = new Person(); $person->firstName = "John"; $person->age = 42; ``` In case if constructor exists, multiple property assignment just rewrites values, so it could even be something like following: ``` class Person { public function __construct( public string $firstName, public int $age ) { } $person = new Person("Name", 43) { firstName: "John", age: 42 }; } ``` However, the syntax is the question and it could be done similarly to: https://wiki.php.net/rfc/compact-object-property-assignment Also, such "functionality" could be reused for cloning and maybe for other appropriate cases: ``` $person = new Person { firstName: "John", age: 42 }; $person2 = clone $person { firstName: "John Doe", age: 43 } ``` --- Taras Chornyi --000000000000d741520628745d54 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div dir=3D"ltr">Hi Internals,<div><br></div><div>I would = like to discuss the possibility of assigning=C2=A0multiple public propertie= s at once during the class initialization.</div><div>Currently, with constr= uctor property promotion we've already achieved the ability to skip a l= ot of boilerplate code. With property accessors (aka hooks) we could rid of= getters and setters. But often classes need constructors only to set prope= rties. In such cases, constructors become redundant.</div><div><br></div><d= iv>Here are a few examples:</div><div>```</div><div>class Person</div><div>= {</div><div>=C2=A0 =C2=A0 public string $firstName;</div><div>=C2=A0 =C2=A0= public int $age;</div><div>}</div><div><br></div><div>$person =3D new Pers= on {</div><div>=C2=A0 =C2=A0firstName: "John",</div><div>=C2=A0 = =C2=A0age: 42</div><div>};</div><div>```</div><div><br></div><div>The initi= alization becomes "struct-like" but it should=C2=A0not affect the= current state and in general it makes code work like this:<br>```</div><di= v>$person =3D new Person();</div><div>$person->firstName =3D "John&= quot;;</div><div>$person->age =3D 42;</div><div>```</div><div>In case if= constructor exists, multiple property assignment just rewrites values, so = it could even be something like following:</div><div>```</div><div><div>cla= ss Person</div><div>{</div><div>=C2=A0 =C2=A0 public function __construct(<= /div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 public string $firstName,</div><div>= =C2=A0 =C2=A0 =C2=A0 =C2=A0 public int $age</div><div>=C2=A0 =C2=A0 ) {</di= v><div>=C2=A0 =C2=A0 }</div><div><br></div><div><div>=C2=A0 =C2=A0$person = =3D new Person("Name", 43) {</div><div>=C2=A0 =C2=A0 =C2=A0 first= Name: "John",</div><div>=C2=A0 =C2=A0 =C2=A0 age: 42</div><div>= =C2=A0 =C2=A0};</div></div><div>}</div></div><div>```</div><div><br></div><= div>However, the syntax is the question and it could be done similarly to:= =C2=A0<a href=3D"https://wiki.php.net/rfc/compact-object-property-assignmen= t" target=3D"_blank">https://wiki.php.net/rfc/compact-object-property-assig= nment</a></div><div><br></div><div>Also, such "functionality" cou= ld be reused for cloning and maybe for other appropriate cases:</div><div>`= ``</div><div><div>$person =3D new Person {</div><div>=C2=A0 =C2=A0firstName= : "John",</div><div>=C2=A0 =C2=A0age: 42</div><div>};</div></div>= <div><br></div><div>$person2 =3D clone $person {</div><div>=C2=A0 =C2=A0fir= stName: "John Doe",</div><div>=C2=A0 =C2=A0age: 43</div><div>}</d= iv><div>```</div><div><br></div><div>---</div><div>Taras Chornyi</div></div= ></div> --000000000000d741520628745d54--