Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121015 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 77074 invoked from network); 8 Sep 2023 14:59:03 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2023 14:59:03 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9446D1804D0 for ; Fri, 8 Sep 2023 07:59:02 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ua1-f42.google.com (mail-ua1-f42.google.com [209.85.222.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 8 Sep 2023 07:58:59 -0700 (PDT) Received: by mail-ua1-f42.google.com with SMTP id a1e0cc1a2514c-7a50a1d1246so821182241.3 for ; Fri, 08 Sep 2023 07:58:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694185138; x=1694789938; 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=glDZjxFmqCfi3HkBRrBGvus48lCqmVK28/BAC0fe1VA=; b=QcT6w3ZzpAf6Qvrm6jIliOc0WyodDCeqhKuTEImuyyuZdzsr3lk8t2yfcvIxJhy3UO L6y0+WOpBtLw5/OVcRuTFCa+eHcTRZVLPRMETQEImmrndugL5IgZUxLbWOmh2C4H3Fs1 C3YcOY5ceRfMvuX8hp8zpo5LqeQsjzwV7y3+nKSn1aTqZumRMES5skiAwtO6r7xCpQBs XoMo5AWilqRVEltN4F9wQZQbCCNT1GS3DV2ofELGgZOC+DEX3uDN+goiamB1Voo5zb+M N6bUo/E+UEVtfZjnRwDSaKFpY/+DEXzjD/i0pJpcwP1ronUkZP5U/M+SfydK7MSkEAge L4AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694185138; x=1694789938; 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=glDZjxFmqCfi3HkBRrBGvus48lCqmVK28/BAC0fe1VA=; b=sELet5xPPf17g3qr3aA4slB96g5KkvXA3RTrVrHhcXaJgxENoCkD8iGiInV+SO8az/ YIXXjlGQhRaBJK6PtK6ZWNYhsZkHF4Wj+f5RquhJT8PZLcxUnNI4yyyjPHYojylEaE0s 6saUfEJdCr6u3bk+8m9KUE9C0Yq1J/VCbUh20VvILZRniRYdOg9Pwou6/4S4qwIyG0aQ /jZSTf4aGTQATQQxCr8Diy+GGzPtdZnioKZEA2ylx6k1RAkeS0KFbBG84lMUKIn+pOI+ g9juN36MGh+GBymcdrBNHJy46qHuOUE8tIDl4A6hgOqbattJn4/UB03SvwNkdMHNDIEf VHLw== X-Gm-Message-State: AOJu0YyW5NzCUW1I97cZZO3a+d9LugyCy6b1C6vDhNIlw0vQUlvtbnyc o2K9b1iJh6ma4aVfBbOQJ3gvE5yGNpi5q1ubh+s= X-Google-Smtp-Source: AGHT+IGpxIJZU/XxMU+gs0+TqXjYINoeTP3cdY+/eBfNKjoixvm4/OKY1QemcYDDkiiTun6AGXPy42/rAVI0Uj1Eons= X-Received: by 2002:a67:b14b:0:b0:44d:5a92:ec43 with SMTP id z11-20020a67b14b000000b0044d5a92ec43mr2984128vsl.24.1694185138230; Fri, 08 Sep 2023 07:58:58 -0700 (PDT) MIME-Version: 1.0 References: <9f0675b9-0474-52e7-1c0e-1186df15c5c5@online-presence.ca> In-Reply-To: <9f0675b9-0474-52e7-1c0e-1186df15c5c5@online-presence.ca> Date: Fri, 8 Sep 2023 09:58:47 -0500 Message-ID: To: Lanre Waju Cc: PHP Developers Mailing List Content-Type: multipart/alternative; boundary="000000000000591a8d0604da370d" Subject: Re: [PHP-DEV] RFC Proposal: Readonly Structs in PHP From: mweierophinney@gmail.com ("Matthew Weier O'Phinney") --000000000000591a8d0604da370d Content-Type: text/plain; charset="UTF-8" On Fri, Sep 8, 2023, 9:15 AM Lanre Waju wrote: > Allowing Methods in Structs: > Initially, I suggested that readonly structs have no methods besides the > constructor. However, upon further consideration, I believe it may be > beneficial to allow methods in structs. Even PHP enums allow methods, and > considering this, I am open to discussing and potentially having a vote on > allowing methods within structs as part of the RFC discussion. > At that point, a struct would be no different from a readonly class with public properties, meaning we'd have two ways to accomplish the same thing. Considering that a readonly class can already implement interfaces such as JsonSerializable, Iterator, and ArrayAccess, they already solve the bulk of the issues that have been raised in this thread. The main thing of interest that I could see from your proposal was the ability to nest a struct in another struct or a class definition, as that could provide some interesting type safety without the need to declare new classes. This could be even more interesting if it allowed _any_ struct that fulfilled the same definitions: class Post { public function __construct( public readonly struct $author { string $name; UriInterface $uri; UriInterface $avatarUri; }, public readonly string $title, public readonly string $content, public readonly DateTimeInterface $publicationDate, ) { } // ... } struct User { string $name; UriInterface $uri; UriInterface $avatarUri; string $email; } $post = new Post(new User(...), ...); The idea here is that User fulfills the $author struct definition in the Post class; it just has _additional_ properties. If the proposal would allow that, this could be pretty powerful. I would personally stay away from solving the conversion to and from arrays and allowing methods. If users need those, they can either use de/serialization libraries or readonly classes, respectively. Not including them in the initial proposal keeps it more targeted, and demonstrates a language feature that does not currently exist. --000000000000591a8d0604da370d--