Re: [php-src] PR #20903: GH-10497 (const obj->prop write) - language change notes
| From: | Khaled Alam | Date: | Sat, 04 Apr 2026 00:52:34 +0000 |
| Subject: | Re: [php-src] PR #20903: GH-10497 (const obj->prop write) - language change notes | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-130557@lists.php.net to get a copy of this message | ||
Hi internals,
I'd like to bring attention back to this RFC, which has been updated to
v0.2 based on prior feedback:
https://wiki.php.net/rfc/const_object_property_write
Summary: This RFC proposes allowing property write operations (->) on
objects referenced by constants. Currently,
CONST_OBJ->prop = value produces "Cannot use temporary expression in write
context", even though the operation
mutates the object, not the constant binding.
What changed since the initial discussion (Jan 2026):
- Addressed Tim Düsterhus's and Ilija Tovilo's feedback regarding array
constant dim writes. The RFC now explicitly
scopes out dim/array writes and documents the "silent no-op" concern in a
dedicated section. A future RFC can
address whether ASSIGN_DIM on non-pointer OP1 should warn/throw — this is
acknowledged as a valid but distinct
issue.
- Added by-reference passing support (BP_VAR_FUNC_ARG path), with tests.
- Added guardrail tests confirming that dim writes on constants, dim writes
on constant objects, and constant
rebinding all remain unchanged.
- Updated the gh12102_3.phpt test: passing an array constant element by-ref
now correctly throws an Error instead of
silently operating on a temporary copy.
PR: https://github.com/php/php-src/pull/20903
I'd appreciate any feedback before moving this to a formal vote.
Best regards,
__
*Khaled Alam*
On Thu, Jan 22, 2026 at 10:33 AM Tim Düsterhus <tim@bastelstu.be> wrote:
> Hi
>
> Am 2026-01-22 00:33, schrieb Ilija Tovilo:
> > No strong opinions on my part, but I'd like to point out that similar
> > situations already exist.
> >
> > https://3v4l.org/ARMMS#v8.4.14
> >
> > const C = [1, 2, 3];
> > function foo(): array {
> > return C;
> > }
> > foo()[] = 4;
> > var_dump(C);
> >
> > PHP will happily duplicate the array returned from foo() to append 4,
> > without warning. The original array in C remains unmodified. It might
>
> I'm aware. However to me this situation is a little different,
> intuition-wise: A return value already looks and feels like a temporary
> value to me (unless a by-ref return is used, of course, but those are
> rare). An access on a const does not.
>
> > make sense for ASSIGN_DIM to warn/throw if OP1 is not a "pointer"
> > (indirect, reference or object), as then the operation will never have
> > an effect. But it seems to me this is a distinct issue.
>
> That makes sense to me and IMO is a necessary precursor for this.
>
> Best regards
> Tim Düsterhus
>