Re: [PHP-DEV] [VOTE] Readonly properties

This is only part of a thread. view whole thread
  115296
July 5, 2021 11:55 patrickallaert@php.net (Patrick ALLAERT)
Le jeu. 1 juil. 2021 à 12:23, Nikita Popov ppv@gmail.com> a écrit :

> Hi internals, > > I have opened voting on https://wiki.php.net/rfc/readonly_properties_v2. > The vote closes 2021-07-15. > > See https://externals.io/message/114729 for the discussion thread on this > proposal. I think a decent tl;dr is that readonly properties as proposed do > not play well with clone-based withers, so some people believe we should > either improve cloning first, or introduce asymmetric property visibility > instead, which does not suffer from this issue. > > Regards, > Nikita >
With the proposed implementation the following class: class A { public readonly string $name; public function __construct(string $name) { $this->setName($name); } public function setName(string $name) { $this->name = $name; } public function setName2(string $name) { $this->setName($name); } public function setName3(string $name) { $this->name = $name; } } Would behave like this: $a = new A("Initial name"); echo $a->name, "\n"; // echoes Initial name $a->setName("New name"); // Allowed ? echo $a->name, "\n"; // echoes New name $a->setName2("Yet another name"); // Allowed ? echo $a->name, "\n"; // echoes Yet another name $a->setName3("Yet another name"); // Not allowed, although identical to setName()? I find the word "scope" very confusing to understand in:
> A readonly property can only be initialized once, and only from the scope > where it has been declared. Any other assignment or modification of the > property will result in an Error exception.
Is the above behaviour an implementation bug or does the RFC implies that? Regards, Patrick
  115299
July 5, 2021 12:07 nikita.ppv@gmail.com (Nikita Popov)
On Mon, Jul 5, 2021 at 1:56 PM Patrick ALLAERT <patrickallaert@php.net>
wrote:

> Le jeu. 1 juil. 2021 à 12:23, Nikita Popov ppv@gmail.com> a > écrit : > >> Hi internals, >> >> I have opened voting on https://wiki.php.net/rfc/readonly_properties_v2. >> The vote closes 2021-07-15. >> >> See https://externals.io/message/114729 for the discussion thread on this >> proposal. I think a decent tl;dr is that readonly properties as proposed >> do >> not play well with clone-based withers, so some people believe we should >> either improve cloning first, or introduce asymmetric property visibility >> instead, which does not suffer from this issue. >> >> Regards, >> Nikita >> > > With the proposed implementation the following class: > > class A { > public readonly string $name; > > public function __construct(string $name) { > $this->setName($name); > } > > public function setName(string $name) { > $this->name = $name; > } > > public function setName2(string $name) { > $this->setName($name); > } > > public function setName3(string $name) { > $this->name = $name; > } > } > > Would behave like this: > > $a = new A("Initial name"); > echo $a->name, "\n"; // echoes Initial name > > $a->setName("New name"); // Allowed ? > echo $a->name, "\n"; // echoes New name > > $a->setName2("Yet another name"); // Allowed ? > echo $a->name, "\n"; // echoes Yet another name > > $a->setName3("Yet another name"); // Not allowed, although identical to > setName()? > > [...] > > Is the above behaviour an implementation bug or does the RFC implies that? >
This is indeed an implementation bug. Thanks for catching it! The issue here is that the property write gets cached and bypasses the readonly check, which of course shouldn't be happening. Regards, Nikita