Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124480 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 EC5E61A00B7 for ; Thu, 18 Jul 2024 14:03:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1721311521; bh=t3/MdojjKzN3i9Vs+w+JZQEloSi+Xuq0T9T/pN77zX8=; h=References:In-Reply-To:Reply-To:From:Date:Subject:To:Cc:From; b=EZFCcMCvcPqYk0uJdDKExstDRv3ZG0zIxT2oOX6jNDHDdazs6rVfmiX3+dE321PWk DBVcqSWb0tbniPcGOjuo6txZHYp2ahDifF+gVlDdzKyVU4TS8J+bIGzgL6hd/T2qE/ F/ncRpuabmKbfeofL7XUJbWHWoJSF1uLqyQGbzAWllOysq2aY3HuOjYYlRCo6Fn0+l xiCo+8gIfEUJORRognymj8xNWJwUccJC9iuCjYMAjslvSuBMvLnAQaiMbKuf8dihSC zvvf7rqOvxrA+5AeCm/CK+9vCOahjntpiHh+mOEio/bb2szHZCdJfHIdPXRoURQg3T i5qDO+ERagqHg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9136918006F for ; Thu, 18 Jul 2024 14:05:20 +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, 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-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) (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 14:05:20 +0000 (UTC) Received: by mail-lf1-f54.google.com with SMTP id 2adb3069b0e04-52ea34ffcdaso426541e87.1 for ; Thu, 18 Jul 2024 07:03:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721311428; x=1721916228; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :reply-to:in-reply-to:references:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=RYWKpVa2dzg+U0wlI4nFo72t30xNN1hg3pe4I4yYscI=; b=AY4KdebgTwZx0ewtbS24h2Si+qPZozSgyiq7vz6sAVgzF36Pep42IF1eCRbO1Gp8We JCWlnE+4hXXaj1UC4mpHVINB/SA6SKLz6blxqvfDdaZHeNM/L6IvP3jLfraXaSMHEk3C PTblut9Bz0oZUiJwUs3UYiJjh2J2kA+HrIIObhMWHUWlq1WYjQYfTXAhl0zwRz64TKcJ Eqa9XcRnAzeBMEeN5YDdRGUq+RDVS7DMcToPVOi9Ex9YieQ0RwqWQ4Cd44BThoMGfyxH GqfoNYX0Ani4jnRUovk42vt5/yqBkgVQ4+FQhgJma74a4CW4d+HGWL6lBPDi+el7T2cB tJAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721311428; x=1721916228; h=content-transfer-encoding:cc:to:subject:message-id:date:from :reply-to:in-reply-to:references:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=RYWKpVa2dzg+U0wlI4nFo72t30xNN1hg3pe4I4yYscI=; b=JcnVwoVFXPABZEceZRZDBh8XtycwWKAX40zruLT0KaeHDI2vJtI1R4fF4LUdsH+0II cRdtxtIc7O+ku60LNzQ/RWJq4/7aDWRW9sOkFS6mhfZj4bzSLDiR+p0Ct11LVhlWPmWT ThrdTYOcW8G/KNKiXDQ8Qk0Z/TjC/Zyu1zlg1l+FEag7UPpkvHSf7CX6Cj4HaviDKmNY lvnZZqdZA/Szh0PPQBrkAOIfa0B2jZ2nE8h9rQYyf/cOiINdR8Q8VMvo1zVhakC2WHdX crJrdAEqZCvSvEBfs4phsTclNYtUZWzmOAJy4tjmpArp22u56LGCRu8apYsuR5BG3yOB YDXg== X-Gm-Message-State: AOJu0YzU3lyXYnuxmRDv/qEQko/JGcNv9FglWENFhAIRG0Hv+6VkIIwA zDZQIxlB61TNzS7V5DH3M1bB8hWkvkuDIZtvojfLAjWl+sPT5t67hAzJhvbQC4bPhQ+0FfTS86w hdOOxkcraKsQWxE5sE1YP5xImKujv+A== X-Google-Smtp-Source: AGHT+IHLnDFCUcwXEkYsXw0XJbOihyMd/E1IudE/fdpx41FvQ5sptnoL/rfMmVSLV3dLgnp9W3aKyVr9+tacyAvmEX0= X-Received: by 2002:a05:6512:281e:b0:52c:86de:cb61 with SMTP id 2adb3069b0e04-52ee53c4047mr4324991e87.10.1721311427531; Thu, 18 Jul 2024 07:03:47 -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: Reply-To: lilybergonzat+php@gmail.com Date: Thu, 18 Jul 2024 16:03:36 +0200 Message-ID: Subject: Re: [PHP-DEV] Optional constructor body To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: lilybergonzat+php@gmail.com (Lily Bergonzat) I would love to see those improvements as well, however I am surprised we seem to be more inclined to push a more substantial change than a minor one. I feel like the more substantial one would be more likely to break stuff, compared to the minor one, and so I don't see why the minor one would be refused? That said, I am new here, and I do not yet know how things work, so it probably is because I lack experience. On Thu, Jul 18, 2024 at 3:50=E2=80=AFPM Larry Garfield wrote: > > 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, w= hich 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), t= hen 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. Whet= her 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 pr= etty minor improvement and unlikely to pass. It would probably only be wor= th doing if there were other common-pattern-optimizations around the constr= uctor that came with it. Things like auto-forwarding to the parent, or a m= ore compact syntax than a full constructor method, or other things that mak= e 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 (whi= ch 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 PH= P's promoted properties. > > val c: Int =3D 5 // A non-constructor-initialized property. These can h= ave hooks, constructor ones I think cannot. > > init { > // This is the non-promoted part of a constructor body, and runs afte= r 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 paren= t::__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 th= ere'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 sayin= g 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 er= gonomic 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