Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119016 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 59052 invoked from network); 17 Nov 2022 14:47:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Nov 2022 14:47:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 596651804F5 for ; Thu, 17 Nov 2022 06:47:47 -0800 (PST) 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-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 17 Nov 2022 06:47:46 -0800 (PST) Received: by mail-ed1-f52.google.com with SMTP id 21so2868681edv.3 for ; Thu, 17 Nov 2022 06:47:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=8bniUfuzXbY4CHPRE5i6hvQQO1yoT4JmNzUXvWNXflI=; b=k1OCMwTXdzbRaGnOSzrH9+S8exp+1yopwkyZmjrkjGzsWqR+WWAxL0OPgD2lnXjI1Y PErVfjHKrTxoQJvmaVbUNmjaNnqEOEMg51NbXHXWxkw5zYy4BIHkTmfVQI10gpipOUBt 1pHUK5bNmqOZgWagBlqDOYYDWfwi/Eg6EXk8vEtozgmfihCIcICY+DZYdlbXmPnjDEE3 oXjeUFjLAs4g5vtG6WJ/BJQ8n/LJiDUOzQKLzgdSNdJtg4awKMYT9eL21u57qtRyEKF0 mE7C1s7kfPLlnLoa5+0zDg1/iL1o6F5WSUZDQHUM7C+0wueGmJmguYdULusmAFNNObLK N6fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=8bniUfuzXbY4CHPRE5i6hvQQO1yoT4JmNzUXvWNXflI=; b=1lqh6KtE5qxTsQS3NZA+d4ooRSYxgZZsvecQwlikaGMir5bTGy07AD/3LaUNJPb5my xqIL+mnqspzjUm/9ZkAogn+PWcQ4Mjh9f4VwpvC7HYEDGMHnIE1rA7JBzmUe/HKbNxYE 23N6543WK0L/Qq1zOyToHxWJp4Oknnrx17r/7+20kaqJMY7WSSHiLUWsXuZJVmKGyy2l RhMg32TlxxSN5/yl5X5+xn2fIIxBG8JnKaNgV7PJG0znrz/tNUWhv9kwR0ymVH2QAAcd APNxEtPqwlsorIsD3wRa4EoaMq27sh+dhYJSHUYSpQgpGs3mfs/iEm5SfEuwuZEeo8C5 dHEw== X-Gm-Message-State: ANoB5pkQMDFP+8IZD8YYlJc4xeIvRvx0IODsbtzMUvsDE1a6LIsNnfw6 yKqQF0a20K2U72t0iSSUdwoRKkIyOW1cCuVDDthLbVk34x6KJcUt X-Google-Smtp-Source: AA0mqf7HuVXZBHj0QLtOXLcUEymYt14P0y9QRUfD9G+0Ll+2KDAc+lODAoI6KIML5R4fxvkKywfHcfR7q+cC/MtUj/w= X-Received: by 2002:a05:6402:1516:b0:467:7519:6bff with SMTP id f22-20020a056402151600b0046775196bffmr2443163edw.301.1668696465382; Thu, 17 Nov 2022 06:47:45 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 17 Nov 2022 15:47:33 +0100 Message-ID: To: =?UTF-8?B?TcOhdMOpIEtvY3Npcw==?= Cc: PHP Internals List Content-Type: multipart/alternative; boundary="0000000000000ea10205edabaca1" Subject: Re: [PHP-DEV] [RFC] [Discussion] Readonly class amendments From: ocramius@gmail.com (Marco Pivetta) --0000000000000ea10205edabaca1 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hey M=C3=A1t=C3=A9, On Tue, 15 Nov 2022 at 07:30, M=C3=A1t=C3=A9 Kocsis wrote: > Hi Everyone, > > Following Nicolas' thread about "Issues with readonly classes" ( > https://externals.io/message/118554), we created an RFC to fix two issues > with the readonly behavior: https://wiki.php.net/rfc/readonly_amendments > > Since I was reviewing https://github.com/lcobucci/jwt/pull/979 today, I had to put the "why immutability is an LSP requirement" in examples. As explained in https://github.com/lcobucci/jwt/pull/979#discussion_r1025280053, a consumer relying on a `readonly` implementation of a class probably also expects replacement implementations as read-only. I am aware that we lack the `readonly` marker at interface level (would be really neat), but the practical example is as follows: ```php count + 1); } public function value(): int { return $this->count; } } // assuming the proposed RFC is in place, this is now possible: class MutableCounter extends ImmutableCounter { public function __construct(private int $count) {} public function add1(): self { return new self(++$this->count); } public function value(): int { return $this->count; } } $counter1 =3D new ImmutableCounter(0); $counter2 =3D $counter1->add1(); $counter3 =3D $counter2->add1(); var_dump([$counter1->value(), $counter2->value(), $counter3->value()]); $mutableCounter1 =3D new MutableCounter(0); $mutableCounter2 =3D $mutableCounter1->add1(); $mutableCounter3 =3D $mutableCounter2->add1(); var_dump([$mutableCounter1->value(), $mutableCounter2->value(), $mutableCounter3->value()]); ``` This prints ( https://3v4l.org/IDhRY ) ``` array(4) { [0]=3D> int(0) [1]=3D> int(1) [2]=3D> int(2) } array(4) { [0]=3D> int(1) [1]=3D> int(2) [2]=3D> int(2) } ``` I took a simplified example on purpose, just to demonstrate the problem with `readonly` disappearing in child classes. That's really really confusing, buggy, and, from a consumer PoV, broken (LSP violation too, transitively). That said, allowing mutation in `__clone()` is fine. Marco Pivetta https://twitter.com/Ocramius https://ocramius.github.io/ --0000000000000ea10205edabaca1--