Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128175 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 lists.php.net (Postfix) with ESMTPS id 1E1651A00BC for ; Tue, 22 Jul 2025 09:19:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1753175846; bh=vcqHv7/zvc0nUcreOEblL29Y5FaZIbpBWZMpuGdCc/4=; h=From:Subject:Date:In-Reply-To:Cc:To:References:From; b=AzrnRN3nvsYsZ7v2Mp9IcVNT+xdMvhtvTuQdLSD4Ks0yKmqJ6dliuv6ZJAlhyZs2d whyPFwUC7BgJBh8gR/OzPWPZXn8CcDBl7RnnS4RGNsCCD2B3VDAh5ulH62GD/k2EyX xYRELXHQZKOq/gM4TztQYGhJ7/aDH3JaBIk8RWsXla/R4Cs/OQF2tLbzMuO39uz80w ZLFAlFftIA7sJs/lbN1+coeJlKiSFs03PaRzktHlrLF22v2IrXa+/PzehJIV4OQy0T 9wh6SAPiI8u25a/N2eBVTJGqA75JR2OOZh1pj44DpI+Rz2PJ9J+hsu7PKOyZl4fwpZ CiSfrRQjs9O0w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C542B180079 for ; Tue, 22 Jul 2025 09:17:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_40,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.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.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 ; Tue, 22 Jul 2025 09:17:25 +0000 (UTC) Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4560cdf235cso25901985e9.1 for ; Tue, 22 Jul 2025 02:19:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753175949; x=1753780749; darn=lists.php.net; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=nE4yrS8PS2c+2XyUw4c9KuCmL+LRF7dAP62Km9Pr6nI=; b=W+9TBwCeE7cv7GP8YCy7FlDlyn+4FRo6rbSFO0wOZrQpD1bcwEob+EHoXjXpgcKfJ4 SSjC3b1QsPtPbsrbj0jnXODHFJrV3vCyDNAczfDeRd2MzERr1LrR0bgk9Lqrs4ba0Aqe A9tqfYBgsqBJr9E688wlS65RmQFWaSUc28mg5QnAQywxacUnfgGmmK3uVR7KteAqfWAr 97CO1f55lSoP1vizvRn2G0dykpPMQKOuDD/R3xEoHkeuTpRbHNxwL6Ow6anLgCXAz+Dd j066EdhQt9/eyOCcn5zJkCaE79cHTGfSUrdTKYMYN7S0sSQE3Gq7sXj23M9Etke0o+J9 tGTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753175949; x=1753780749; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nE4yrS8PS2c+2XyUw4c9KuCmL+LRF7dAP62Km9Pr6nI=; b=H3oCq2IM8gDyv0zNueZoZuC8dDY/HwZ2wN+4Yd45RMPNHbsSF+q1EXYZ+u9Fav21Ec oqreBNla1tU7xhp6Pkox9U1XE/MZheFVDGSvxTW3+FVoULn6X6cV9POHmgj1a7Kv+ls+ Xz+g6AB9CsQnrWowu+cG6NkKouNwqS+VcrALXh7Ri99/WT4wXhOvSvhOSIoiHsqs8vMx /JfNkR6L140XAJ/+F5ZBrXxnt/7EbsZo2CstoGXR6gv1qBHOP7cwAZFEcW1IJ+6Nu1Ey JeB6fIjndRa6fw4y4xNRHbSQ+4Q/1fSakogVtpi5e6lcxPlj6qoy97coVGTa3U0voBjN 26lw== X-Forwarded-Encrypted: i=1; AJvYcCWnN+yGfoSW4ssJr9HNMm+XrEfqnB9dSOfdW2u6bg+J0r1lyR9g1LjH96QoUyMQQ802rG+iNlil2iQ=@lists.php.net X-Gm-Message-State: AOJu0YwMtYfZCLTQkWO3OIf6QNOUKujFyCwA5Z0MGXHQMi+pbUpDglzU szRWoDjD9ehnyrlbFGiz3j3Eatb+xLtvDEWb2X419yKm8jRZ7z0cwCfe X-Gm-Gg: ASbGnctepCzJHy2TEsEYutcdw6GMjIZNYg0H4CK3HqUXNHzBc707866+ER+5IpkXVCD LFBgFUSXP9c/Ra7Su4TnZo6x5P5zXgbrYMAYHHojXoA2RdLz8RKA5GN4nb1StWtgPu6flPnDqCD qVulzjccZVoZoYXGDVK+bLoBWSS35oFue+VARCnzHXx+FeEHvRbNAa/EqNDcPcJqtkfDADPMTxW qeJEZkAdc3H8M8kUjOoxIYygKgyq3Xj46hBbOC33ufXlUFRHxdbsSkVdcqSbs+S2hUGWnA0iQkZ +18qblxZ7UXwNS6Kma7khLdfWV++gUrA2jeH88aVvmtLjX1aHpVefPvb1Kv1+kPsVdIDsBsaStI bAFnrFOIIdGf0Zxo1M2xNrUfsRuU60Hpjzs7CxQEH X-Google-Smtp-Source: AGHT+IFa4XSxQAIgwRpkkOx3AC3qGM2FkphgjVQUZCmmX5JRx4G4kWei2fkJL/T5r3avnNv7xEolfA== X-Received: by 2002:a05:600c:c0d6:b0:441:d4e8:76c6 with SMTP id 5b1f17b1804b1-4563555411amr153577395e9.30.1753175949242; Tue, 22 Jul 2025 02:19:09 -0700 (PDT) Received: from smtpclient.apple ([89.249.45.14]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4563b5c7e8bsm122756225e9.14.2025.07.22.02.19.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Jul 2025 02:19:08 -0700 (PDT) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_D7A867A9-A352-4ACE-A822-D8E2666C567E" Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.600.51.1.1\)) Subject: Re: [PHP-DEV] [RFC] Readonly property hooks Date: Tue, 22 Jul 2025 11:18:57 +0200 In-Reply-To: <4567d926-aaed-44a8-9cd6-68e8e12742f4@app.fastmail.com> Cc: erictnorris@gmail.com, php internals To: Rob Landers References: <378cd8b0-c4a8-4e53-b0bd-e9e4f30a454d@app.fastmail.com> <77128196-8D53-40D6-9BB7-77160AD71ED9@gmail.com> <247034b6-04cf-4f3e-a145-a30171af8c12@app.fastmail.com> <5a51401a-450b-46f0-8467-88d31efecb9c@app.fastmail.com> <4567d926-aaed-44a8-9cd6-68e8e12742f4@app.fastmail.com> X-Mailer: Apple Mail (2.3826.600.51.1.1) From: claude.pache@gmail.com (Claude Pache) --Apple-Mail=_D7A867A9-A352-4ACE-A822-D8E2666C567E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Le 22 juil. 2025 =C3=A0 09:43, Rob Landers a = =C3=A9crit : >=20 > You can also implement this today, without using readonly: >=20 > class Foo { > public int $bar { > get =3D> $this->bar; > set =3D> empty($this->bar) ? $this->bar =3D $value : throw new = LogicException("nope"); > } > } >=20 > https://3v4l.org/2JagR#v8.4.10 >=20 Your code is buggy, because `0` is =E2=80=9Cempty=E2=80=9D: https://3v4l.org/iUPvW#v8.4.10 > Should I be able to mark this class as readonly? I would think so. I don=E2=80=99t think so. If you want to *document* the intended invariant, you can put a = @readonly tag in a phpdoc comment. Adding a `readonly` keyword should *enforce* the invariant; the added = value is that it would choke on bugs like the one you wrote just above, = making it debugging much easier. > The readonly keyword simplified that greatly, however, readonly has = been neutered compared to regular classes in the last couple of = versions. There are so many edge cases and non-implemented features with = them -- mostly due to this exact argument you are making -- that they're = nearly worthless to actually define immutable value objects in today's = PHP. >=20 There are several issues with the readonly feature, mostly because of = the =E2=80=9Cworse-is-better=E2=80=9D philosophy. But we can slowly = correct the main issues instead of making them worse. =E2=80=94Claude --Apple-Mail=_D7A867A9-A352-4ACE-A822-D8E2666C567E Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

Le 22 juil. 2025 =C3=A0 09:43, Rob Landers = <rob@bottled.codes> a =C3=A9crit :

You can also = implement this today, without using readonly:

class Foo = {
  public int $bar {
    get =3D> = $this->bar;
    set =3D> empty($this->bar) ? = $this->bar =3D $value : throw new LogicException("nope");
  = }
}



Your code is = buggy, because `0` is =E2=80=9Cempty=E2=80=9D:

https://3v4l.org/iUPvW#v8.4.10=


Should I be able = to mark this class as readonly? I would think = so.

I don=E2=80=99t think = so.

If you want to *document* the intended = invariant, you can put a @readonly tag in a phpdoc = comment.

Adding a `readonly` keyword should = *enforce* the invariant; the added value is that it would choke on bugs = like the one you wrote just above, making it debugging much = easier.


The readonly keyword simplified that greatly, = however, readonly has been neutered compared to regular classes in the = last couple of versions. There are so many edge cases and = non-implemented features with them -- mostly due to this exact argument = you are making -- that they're nearly worthless to actually define = immutable value objects in today's = PHP.


There are several = issues with the readonly feature, mostly because of the = =E2=80=9Cworse-is-better=E2=80=9D philosophy. But we can slowly correct = the main issues instead of making them = worse.

=E2=80=94Claude

<= br>
= --Apple-Mail=_D7A867A9-A352-4ACE-A822-D8E2666C567E--