Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124484 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 0B5C71A00B7 for ; Thu, 18 Jul 2024 17:08:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1721322588; bh=SAx8ByonBycsKwzYsH1tnNJH02zU7qa0b0NBoARfY7M=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=V3aI//1+479SVuDUzsGApqxT8euLlE02kLUr7W75p2Z7dqnxDtQ04wfmrg4gNO2X4 d97q2Pwyq9c9aISmm+slw9W608ABnWkFARS+4nROQ9HASijhseUy4Xw/YtM/QS8smY 7P5n7frZB7pUc878mkB1LT3anqgrREDGdkut6YHtcgOy9XUo6A9VuIvC6JYPZfu+ZH v3aCmF9WaaK8ehiwmIQT/898x5gRQSuiLd308UJ0JUMGKjmrzAACTDbgFf3737xuti o89KArM7ibFN6928AWnTQTjLTyxav5W/BkyvlHPQJoze3cG9fRchLFcYYVF00S1S0f 4Q821g9+gI8rg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 88012180052 for ; Thu, 18 Jul 2024 17:09:47 +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: Received: from mail-yb1-f176.google.com (mail-yb1-f176.google.com [209.85.219.176]) (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 ; Thu, 18 Jul 2024 17:09:47 +0000 (UTC) Received: by mail-yb1-f176.google.com with SMTP id 3f1490d57ef6-e04196b7603so1059826276.0 for ; Thu, 18 Jul 2024 10:08:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721322495; x=1721927295; 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=txZeaF6SCjXqQ7/JA+kdQBPDms+AdZzDpBOO31OnB4E=; b=f8dNib2bbhvnb+CB/ykYdtegAWj4VlML3CwUBeQjXBNqhxUsAyAudbGBazoyAmb9Vt 4vA50MASrzmazQw1JdXegsLzdy+daVQi08e4NABVCHCOoTnYFkB6U415dp7hHlgkjA6L I8FK3SiH/Yc0kTN2pi87aDh8h+hFckAkkwJrk/UVfH/2er0cU92fyQ/lwPX1B9IfEpsq mLIEbYYADQKQTJg08I0Dqfb+k8VpUfCg0lPoOcibnlccbhXFK7n/hIPwbO9e5v131RMN pgJoks9akHfwmd6Ht6gaRmvg+SAo6Me97sxR3QKIv/wffQBzy4qUFItFw8PJnMsT8RDC 9NqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721322495; x=1721927295; 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=txZeaF6SCjXqQ7/JA+kdQBPDms+AdZzDpBOO31OnB4E=; b=N8W80y221OuQXEDdFKrYRmMPRbzRD+UVYsto5sdtHkoO7HxC2C3UfABmkSI+KGeMrS Y9tobGmwWHjT3OkzfzGdaom9R7q1QzcJuGyNNLDM4yCVkFQWK/EXRFtL0W38X3sMrKBy /ZFuUF8TOmAZD42Z6ionaVHsW6xR9ZcbkG5iQdIO71QnJHn30iCvYf8qVIGsWgdlVz9z kjOD56YyvRzQ/3fvaIbhd9FX1CGF8No3H17hP7TZBRxRsFRX6iG+tr+p3mCGDkNwCOr9 s0TF0Jwbye2b/05h6RtHPaACwRMvonkDzEdGcMj9tvcNMTO8z/rnDlEekZFwlFyYfsWr gqPA== X-Gm-Message-State: AOJu0YxKbkL1iMzHIhW2tRm/2MldIUiDqgjt2jyy6vg2YTKmXb3dxX8o cbcHWB3tB5W56r3PcFskWieIC1MPM7BOSCtfdiX8fnDLkFyA+69BMLRE7cQRRVlyn1Kt0uNDqBv OeJoqHQkcB5oYi4rZ0SeW4L2fN4+KYYSi X-Google-Smtp-Source: AGHT+IGKAe3onQ9SLTA/2LY/WsfAIZcMbZZ0154tQkks1WxaeTFU6AWq8MJhkJozV+NKRrR+/FLV1UmCZHiSxv67y9U= X-Received: by 2002:a05:6902:18c5:b0:e06:6e0c:5e0 with SMTP id 3f1490d57ef6-e066e0c08bemr2856935276.38.1721322495554; Thu, 18 Jul 2024 10:08:15 -0700 (PDT) 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: Thu, 18 Jul 2024 19:08:03 +0200 Message-ID: Subject: Re: [PHP-DEV] Optional constructor body To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000e4412f061d889fac" From: olivernybroe@gmail.com (Oliver Nybroe) --000000000000e4412f061d889fac Content-Type: text/plain; charset="UTF-8" On Thu, 18 Jul 2024 at 15:48, Larry Garfield wrote: > > 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 > Sorry about the top posting and thank you for your feedback. I'll take that into account. I have used Kotlin myself for many years and love how concise the syntax is there. I'll take a look at other languages and see if I can come up with a more concise syntax that still feels like PHP. Else I might try out creating this small RFC first as a good introduction to the flow, even though it is unlikely to pass. Best regards Oliver Nybroe (he/him) --000000000000e4412f061d889fac Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, 18 Jul 2024 at 15:48, Larry Garfield <larry@garfieldtech.com> wrot= e:

Please don't top-post.

Since the last time this came up, PSR-12 has been replaced with PER-CS, whi= ch 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 th= e previous symbol, separated by a space.

cf: https://www.php-fig.org/per/co= ding-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 resolve= d.=C2=A0 Whether that changes anyone's mind about whether this should b= e 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.=C2=A0 It would probably = only be worth doing if there were other common-pattern-optimizations around= the constructor that came with it.=C2=A0 Things like auto-forwarding to th= e parent, or a more compact syntax than a full constructor method, or other= things that make writing a "pure data" product type easier rathe= r than just s/{}/;/

I don't know what those could look like.=C2=A0 As a data point, in Kotl= in (which is what my day job is now), constructor properties are always pro= moted, essentially.=C2=A0

class Foo(val a: String, val b: String) { // This is the equivalent of PHP&= #39;s promoted properties.

=C2=A0 val c: Int =3D 5 // A non-constructor-initialized property. These ca= n have hooks, constructor ones I think cannot.

=C2=A0 init {
=C2=A0 =C2=A0 // This is the non-promoted part of a constructor body, and r= uns after the properties are assigned.
=C2=A0 }
}

In case of inheritance, there's dedicated required syntax for forwardin= g 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.=C2=A0 That means a great m= any "value objects"/DTOs, etc just look like this:

class Foo(
=C2=A0 val a: String,
=C2=A0 val b: String,
)

Which would be equivalent to PHP's

class Foo {
=C2=A0 public function __construct(
=C2=A0 =C2=A0 public readonly string $a,
=C2=A0 =C2=A0 public readonly string $b.
=C2=A0 ) {}
}

cf: https://kotlinlang.org/docs/classes.html

To be clear, I'm not suggesting PHP just copy Kotlin directly.=C2=A0 I&= #39;m saying that if we want to improve the constructor syntax for common c= ases, which I am open to, we should be looking to do something more substan= tial and ergonomic than just replacing {} with ;, and we could probably get= some good inspiration from other languages in our family.=C2=A0 (Java, Kot= lin, C#, Swift, etc.)

--Larry Garfield


Sorry a= bout the top posting and thank you for your feedback. I'll take that in= to account.=C2=A0
I have used Kotlin myself for many years and love how = concise the syntax is there. I'll take a look at other languages and se= e if I can come up with a more concise syntax that still feels like PHP.=C2= =A0

Else I might try out creating this small RFC first as= a good introduction to the flow, even though it is unlikely to pass.
=


Best regards
Oliver Nybroe (he/him)=C2=A0=
--000000000000e4412f061d889fac--