Re: [RFC] Readonly Variables
| From: | Kamil Tekiela | Date: | Tue, 24 Feb 2026 19:50:37 +0000 |
| Subject: | Re: [RFC] Readonly Variables | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-130159@lists.php.net to get a copy of this message | ||
On Tue, 24 Feb 2026 at 19:32, Larry Garfield <larry@garfieldtech.com> wrote:
>
> On Tue, Feb 24, 2026, at 12:52 PM, Joshua Rüsweg wrote:
>
> >> I would prefer starting with readonly parameters as I feel that would
> >> bring the most value.
> >
> > I find the idea of readonly parameters interesting and have already
> > given it some thought. However, it is out of scope for this RFC, as I
> > wanted to keep the initial proposal focused and manageable. I have
> > already mentioned it in the Future Scope section of the RFC as a
> > potential natural extension that could be addressed in a separate RFC
> > in the future.
>
> A slight tangent here, but what would readonly parameters do?
>
> If you pass something by value, then already you cannot modify the value in the caller. If the
> value is large, it's already kept efficient by copy-on-write.
>
> If you pass by reference, then presumably you *want* to modify the value in the caller.
>
> If the value is an object, then as already discussed, its internals are mutable either way so
> you gain little, but the function cannot change the identity in the caller anyway.
>
> So I don't see what readonly parameters would even accomplish. The thing they accomplish
> in JS or Java or C simply doesn't apply in PHP.
>
> I'm still undecided at the moment on readonly variables.
>
> --Larry Garfield
A readonly parameter would mean that it would be impossible to reuse
the variable within the function for something else. It's not uncommon
for a function to preprocess the argument and still make use of the
original value. A readonly modifier would prevent accidental
overwrite.
function foo(string|array $data) {
if (is_string($data) ) {
$data = [$data]; // Original argument lost
}
// ....
return is_string($data) ? $result : [$result];
)