Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118564 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39021 invoked from network); 5 Sep 2022 10:06:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Sep 2022 10:06:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8948E1804AB for ; Mon, 5 Sep 2022 03:06:03 -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-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (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 ; Mon, 5 Sep 2022 03:06:03 -0700 (PDT) Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-3454e58fe53so9110357b3.2 for ; Mon, 05 Sep 2022 03:06:03 -0700 (PDT) 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; bh=0Z3m6omMy3D235euWPzvgb7KD/4Gtx2Dqh0uZjB5HP8=; b=UFv+o4WzPGcynQFzv97NvlidKHsJKH2JwO7mQ7kq6QsbqhIkUpubEq0XOFb5vTeTJk 835yoPE0ip6Kgmh+FR98mtMKZ0CkrMrcY+HoiEYdpbpI/dmOqZaag3eaHWlviYyo2EEb Zc/ZdtWlmbtQ3xGjsDJ/GoLtLs5EndWAbI0E8+yzmA/hUXfgM3ZXLd2VrfWH6ULm+R1N yuBEovmrd+FNEz8U6228KugmTDWUXJL4nLf/rI+QBrkx8wuX1ri8vJS7AgZGBSMf6EJN /HlfqpuwlnMGO7H1qD23qjHBAFkWhgravLm/JQ6uCIEOnQbGmVFsYoCW4gjXFJSU4UQM sQ3A== 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; bh=0Z3m6omMy3D235euWPzvgb7KD/4Gtx2Dqh0uZjB5HP8=; b=YlJ2+oISYeF0J+/zMSRLGZOe2+QSX3faTjQGGQzkEjtD111Z7D8iCmmMogEkgpHfRv jbDuEB36Qfd4RDObTJHJmXHop7ilnvy5lrOGBVFYtNZ4gDZLNTgEfJ+9Q0YNbpgOMG5H 0utDsrutL9Ec9drM56eWB5xeJ9YWrO1ysufuYuq1L/XwC8HqnHkeUIWMhxIyS6oC4Ypc sDzFWgLx+A5f7OIAN10uM37Ac1sSJy5YleyOVR+LN9hL2IyJbxxP27xhTDvIRIp4kpVZ UC0zUpCKBLxg+eQ1spc1+0HGPXW4+s16Br9ezxzD93ppFHW3iTs71R+rJKPe6xfj2sD/ DV8w== X-Gm-Message-State: ACgBeo3wwe5NxWz6tCBU6wHaoxQ7Uf/e6Jq23yr8oxSUhhHgArGEL7bo TQkJlC1qCpS/NnDZN+7jnGESrN4tqu2Zcg4awuxsUescgtw= X-Google-Smtp-Source: AA6agR6R4HOMk6rm9dzQqTo6Kk9TP0cheE2ZONhPtri9FTLIcsowhgtgYrfH1RrhqjPBjWSgG6Gud0PDx3cW29LC4xg= X-Received: by 2002:a81:b89:0:b0:345:17e3:bfac with SMTP id 131-20020a810b89000000b0034517e3bfacmr7202931ywl.157.1662372362534; Mon, 05 Sep 2022 03:06:02 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 5 Sep 2022 12:05:51 +0200 Message-ID: To: =?UTF-8?B?TcOhdMOpIEtvY3Npcw==?= Cc: PHP Internals List Content-Type: multipart/alternative; boundary="00000000000027443b05e7eb3acd" Subject: Re: Issues with readonly classes From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --00000000000027443b05e7eb3acd Content-Type: text/plain; charset="UTF-8" Hi Mate For 2.: static properties are useful e.g. to cache some shared state (info >> extracted from reflection in my case) and I really don't see why readonly >> classes should forbid static properties. Readonly is only useful for >> instances, because it gives them immutability, but what's the link with >> static properties? Note that this is less important than the previous point >> as it's always possible to store shared global state in another class. But >> still, this feels arbitrary to me. >> > > First, I'll answer to your question about static properties, because the > answer is simple: as readonly static properties are not supported ( > https://wiki.php.net/rfc/readonly_properties_v2#restrictions), I had to > decide what to do with static properties of readonly classes ( > https://github.com/php/php-src/pull/7305#issuecomment-971381308), so > Nikita and I agreed that forbidding them is the better option until we add > support for static readonly properties. In my opinion, being able to set > readwrite static properties for readonly classes would be very > counterintuitive. > Thanks for the precision, works for me As I mentioned, there are ways around anyway (storing in another class or in a static variable.) But I'm also wondering about B: the RFC doesn't contain any rationale about >> why this restriction exists (it says "Similarly how overriding of readonly >> properties works", but the similarity is quite weak, readonly props don't >> apply to other properties of child classes.) If there is no compelling >> reason to have this limitation, it should be removed IMHO. Basically, this >> would mean that child classes of readonly classes wouldn't have to be >> readonly themselves (but the existing properties on the parent would have >> to be of course.) > > > Regarding your main question: I understand your problem with readonly > classes, and I'd be happy if we found a solution which fits your use-cases > and keeps consistency for the engine at the same time. To give you more > context about the inheritance related restriction in the RFC: I went with > forbidding the extension of readonly classes by non-readonly ones so that > the invariants of the parent (no dynamic or mutable properties are allowed) > don't occasionally get violated by the child. However, I cannot think about > a proper scenario now where this would cause any problems... I'm wondering > if anyone could come up with one? > Not only can I not find where removing this invariant would be a problem, I also cannot think of where this invariant would be useful. I don't see what I can build on top of it... > And at last, regarding the interaction of readonly properties with > __clone(): I'll definitely think more about this topic in the following > weeks. Currently, it seems to me that readonly properties should be able to > be reinitialized once *during* cloning, but not afterwards. Is this in line > with what you want to achieve? If we once manage to add support for the > "clone with" construct then readonly property reassignment(s) due to "with" > arguments should also be allowed. But I'll need more time to also consider > any undesirable side effects/edge cases, which could apply, should we > settle on the approach described above. > During cloning only, yes, and we don't need any new syntax to allow this (we might need one to make it easier to write wither APIs, but that's another topic.) That's exactly what I'm trying to achieve in https://github.com/php/php-src/pull/9403. For the record, I tried 3 approaches to manage the transient state that is required to flag readonly properties as once-writeable during cloning. I tried using zval.u1 but I realized this is a read-only area right?, then tried using zval.u2.extra but this is not a bitfield, then zval.u2.property_guard but without luck. I didn't see any blocker either in the engine, so the only reason why I failed is because my C-related skills are too weak and I'd need help to figure out a proper patch. Nicolas > --00000000000027443b05e7eb3acd--