Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124478 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 F15921A00B7 for ; Thu, 18 Jul 2024 13:47:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1721310526; bh=f3wvFWsbF4ZVM05OM7Cg3HW+eJt99cxA+j2VyPKsU44=; h=In-Reply-To:References:Date:From:To:Subject:From; b=LBb/iGHnZGWsTSe+EOXAbIL4b0pdvjEJgOoguXGwKzWsXZLRQLrX1Q6A67a8w0JjD IEUQh1CXvUabNtDMN4cyYsvuUIFhCMOBYCMweFOwAIUN37F0h4p8HxyPkajq8ZR/55 IbPkxSCxagUlHYn/5OPARY1MActd/VAvv9qfUHi72AoQvVAMlgetnjDkw/SDK5XSz7 2zdRgPzmMK8Byy2Kr4ZCANa5TiDPK+0ELuRS3qIrcG5Yup4Z3K7FoL27PGy0d691DQ 4uSEJAHWfLCMB5W5kGMmD5E8tAmoQa4EHgdIDHOiAK4QIQTsSmtJwl9NKs7e0ig0zP x7AAkdiUBfUJg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7EC2D180B39 for ; Thu, 18 Jul 2024 13:48:45 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh2-smtp.messagingengine.com (fhigh2-smtp.messagingengine.com [103.168.172.153]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 18 Jul 2024 13:48:45 +0000 (UTC) Received: from compute9.internal (compute9.nyi.internal [10.202.2.228]) by mailfhigh.nyi.internal (Postfix) with ESMTP id EA95611401C2 for ; Thu, 18 Jul 2024 09:47:13 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute9.internal (MEProxy); Thu, 18 Jul 2024 09:47:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc: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=fm2; t=1721310433; x= 1721396833; bh=smX9bg/vXxTMIZXFeKvcu3hzA+tddV95it9h1lE+4Es=; b=E J0UIJ2445+gR+orM1zXBu53U7ks8BxNex3uZb5yYVgJ3NYYb8v/ffkcazPE9jaQr 7E538b6Hpukvt8JMi1+RYdxLaPiauovvFHQfo063mieLbWGwtGIBsyknPkpK8fNG vIFg6UIzHSNoHlHn50GN61C7vbqH3E2P6qMmjJOxQKfnICFDv0QILSxgbQF9Pbv9 zOhUqT8JSOvaReM7p546b6gEL8Gei0JO08vHbIlWW391+7n/5tu/rZe8OXhvt66T rZazEtNbEJ1jrZmybe6GYgOsBPIw01cY3r4YreuGnclX8H+cvnMljMFNGasyS0N1 HtOGHrb6E7BBkazArwAEw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1721310433; x=1721396833; bh=smX9bg/vXxTMIZXFeKvcu3hzA+td dV95it9h1lE+4Es=; b=Ths3hJs/GoJW/EckulTTgo+MJ2y1d6AF4n2T8zZlh30f BahCwp+OUQsXQa6uTf1g+imvxqqxrdiW3P/cSkr/W5JJBRV8FRp8TCQTbmXloAhQ C6XRv+yMrseyrYTjmtre40Dql8gyT79ZnmnTWE4fiO1V8Q5NyXHbZvQ9GfoQ1PV0 Msou1RHIyHhDIRHxq58VY5OvqT6MezKEbBv9Of3Fr1+Np1Uhyg8M5HWW48Ix7TrC fGY9T/8D1lNJLV58lx/vqukd3QLlKEJAyGUnuHnwEWGAYMrwKVwxuLsfiuxpBbs6 T4rbUom/mbELPVEOPq41iRCMg7HiEfIWdrYg7tiBfw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrgeelgdeikecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgrrhhr hicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh eqnecuggftrfgrthhtvghrnhepgfdttddvueetueffheevkeelffeljeehveeiledufeek leekuefggeffkedtvdffnecuffhomhgrihhnpehphhhpqdhfihhgrdhorhhgpdhkohhtlh hinhhlrghnghdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgr ihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 890A81700093; Thu, 18 Jul 2024 09:47:12 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-568-g843fbadbe-fm-20240701.003-g843fbadb Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Message-ID: In-Reply-To: References: Date: Thu, 18 Jul 2024 13:46:42 +0000 To: "php internals" Subject: Re: [PHP-DEV] Optional constructor body Content-Type: text/plain From: larry@garfieldtech.com ("Larry Garfield") On Thu, Jul 18, 2024, at 10:11 AM, Oliver Nybroe wrote: > Thanks for sharing previous discussions, I will definitely take a look > at those before writing up the RFC. > > >> If you do with to go with an RFC, I'd like to see if your proposal > addresses whether this syntax should implicitly call > `parent::__construct()`, and if a semi colon is expected or not > (`public function __construct(public int $foo);` vs `public function > __construct(public int $foo)`). > Thank you, these are very valuable points to address in the RFC. > > I can definitely feel that there will be some mixed opinions about > semicolon vs no semi colon. > > > Best regards > Oliver Nybroe (he/him) Please don't top-post. Since the last time this came up, PSR-12 has been replaced with PER-CS, which as of 2.0 now says: > If a function or method contains no statements or comments (such as an empty no-op implementation or when using constructor property promotion), then the body SHOULD be abbreviated as {} and placed on the same line as the previous symbol, separated by a space. cf: https://www.php-fig.org/per/coding-style/#44-methods-and-functions (I... suppose technically it doesn't mention classes, but I've been doing it for empty classes too.) So the "coding style" part of the previous issue has been resolved. Whether that changes anyone's mind about whether this should be done or not is up to them to decide. Personally, I'd probably vote for it if it came up, but I agree it's a pretty minor improvement and unlikely to pass. It would probably only be worth doing if there were other common-pattern-optimizations around the constructor that came with it. Things like auto-forwarding to the parent, or a more compact syntax than a full constructor method, or other things that make writing a "pure data" product type easier rather than just s/{}/;/ I don't know what those could look like. As a data point, in Kotlin (which is what my day job is now), constructor properties are always promoted, essentially. class Foo(val a: String, val b: String) { // This is the equivalent of PHP's promoted properties. val c: Int = 5 // A non-constructor-initialized property. These can have hooks, constructor ones I think cannot. init { // This is the non-promoted part of a constructor body, and runs after the properties are assigned. } } In case of inheritance, there's dedicated required syntax for forwarding to the parent: class Foo(val a: String, val b: String) : Bar(b) { // equivalent to parent::__construct($b) } You can also make the constructor private (etc.) with more explicitness: class Foo private constructor(val a: String, val b: String) {} Of note, if there's no constructor then the parens are omitted, and if there's no body then the {} body is omitted. That means a great many "value objects"/DTOs, etc just look like this: class Foo( val a: String, val b: String, ) Which would be equivalent to PHP's class Foo { public function __construct( public readonly string $a, public readonly string $b. ) {} } cf: https://kotlinlang.org/docs/classes.html To be clear, I'm not suggesting PHP just copy Kotlin directly. I'm saying that if we want to improve the constructor syntax for common cases, which I am open to, we should be looking to do something more substantial and ergonomic than just replacing {} with ;, and we could probably get some good inspiration from other languages in our family. (Java, Kotlin, C#, Swift, etc.) --Larry Garfield