Re: Allow hooks in `readonly` promoted properties

From: Date: Thu, 05 Jun 2025 06:12:42 +0000
Subject: Re: Allow hooks in `readonly` promoted properties
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-127606@lists.php.net to get a copy of this message
Hey internals,

> On 4. Jun 2025, at 20:19, Nick <php@nicksdot.dev> wrote:
>> I believe at the moment that RFC text is all there is. :-)  I don't know that
>> it's worth opening a discussion without at least a mostly-done implementation.  Also, Ilija is
>> rather busy on other tasks at the moment, as am I.  (Unless someone else wants to jump in to
>> implement it, which would be fine.)
> 
> People often say “you can just do things”. So I did, and tried to contribute the code for
> your existing RFC text:
> 
> https://github.com/php/php-src/pull/18757

Would this be expected to work? Can interface properties be declared readonly?
interface Cleaned
{
    public readonly string $clean { get; } // has readonly
}

class Something implements Cleaned
{
    public function __construct(
        public readonly string $clean {
            get => trim($this->clean);
        }
    ) {}
}

$u = new Something('   Yoda   ');
var_dump($u->clean);
In my current implementation it would throw:

> Fatal error: Hooked virtual properties cannot be readonly in 

I’d appreciate input here.
Thanks in advance!

Cheers,
Nick





Thread (14 messages)

« previous php.internals (#127606) next »