Table of Contents

PHP RFC: Allow Object Property Writes on Objects Referenced by Constants

Introduction

This RFC allows writing to object properties (and isset() / unset() on properties) when the object instance is referenced by a constant.

PHP constants are immutable in the sense that the constant binding cannot be changed. However, objects in PHP are mutable by design: mutating an object property does not rebind the constant, it only updates the state of the referenced object.

Today, PHP rejects CONST_OBJ->prop = ... with a fatal error (“Cannot use temporary expression in write context”). This RFC proposes permitting property write operations in this specific scenario, aligning behavior with user expectations while keeping the constant binding immutable.

<?php
const OBJ = new stdClass();
 
OBJ->value = 123;
var_dump(OBJ->value); // int(123)
 
unset(OBJ->value);
var_dump(isset(OBJ->value)); // bool(false)
?>

This applies both to global constants (const / define()) and to class constants (Cls::CONST).

Proposal

This RFC proposes to allow property write operations where the base expression is a constant that evaluates to an object.

The change is intentionally narrow:

Specifically, the following operations become valid:

Semantics

Note on Array Constants

A related but distinct scenario exists with array constants:

<?php
const ARR = [1, 2, 3];
$ref = ARR;
$ref[0] = 9;
var_dump(ARR); // [1, 2, 3] — original unchanged
?>

Because arrays use copy-on-write semantics, modifying the copy has no effect on the original constant. Similarly:

<?php
const C = [1, 2, 3];
function foo(): array { return C; }
foo()[] = 4;
var_dump(C); // [1, 2, 3] — unmodified, assignment is on a temporary
?>

These dim-write cases involve assignment to temporaries that silently have no effect. Whether such no-effect writes should produce a warning or error is a valid concern but is out of scope for this RFC and may be addressed separately (e.g. warning/throwing on “ASSIGN_DIM” when OP1 is not a pointer — i.e. not an indirect, reference, or object — as the operation can never have an effect). See the Future Scope section.

Non-goals / Explicitly Unchanged

This RFC does NOT change:

Implementation Details

The compiler marks the constant AST node rather than emitting the fetch directly. zend_delayed_compile_prop() in Zend/zend_compile.c calls zend_mark_const_object_fetch(), which walks down through any ZEND_AST_DIM nodes and sets a ZEND_CONST_OBJECT_FETCH attribute on a ZEND_AST_CONST or ZEND_AST_CLASS_CONST base. zend_delayed_compile_var() then handles those two AST kinds when the attribute is set, so the constant is fetched at runtime and the property write targets the real object.

zend_emit_fetch_constant() and zend_compile_class_const() take the fetch type: in read context (BP_VAR_R, BP_VAR_IS) the result stays IS_TMP_VAR as before, and in write context it is IS_VAR. Because a constant may still be substituted at compile time (true, null, persistent constants, same-file class constants), a compile-time-evaluated constant in write context is wrapped in a ZEND_QM_ASSIGN to materialise it into an IS_VAR; the property opcodes have no handler for an IS_CONST container operand.

Two supporting changes were needed. zend_optimizer_update_op1_const() now refuses to substitute a constant into op1 of the object-write opcodes (ZEND_ASSIGN_OBJ, ZEND_FETCH_OBJ_W, ZEND_UNSET_OBJ, ZEND_PRE_INC_OBJ and friends), which would otherwise reintroduce the invalid operand during optimisation. And zend_can_write_to_variable() accepts a constant at the base of a PROP/DIM chain when the chain contains at least one property fetch, so that destructuring assignment and by-reference foreach can target a constant object property; a write to the constant itself, or to a dimension of it, still targets a temporary and remains illegal.

Why This Brings Value

This feature addresses a common surprise: PHP already permits objects in constants (since PHP 8.1), and developers naturally expect to mutate the object's state through the constant reference. The current fatal error is not about constant immutability per se, but rather about an overly broad “temporary write context” restriction in the compiler. Narrowly enabling property writes reduces friction, improves ergonomics, and matches the runtime model of objects without increasing language complexity for other write contexts.

Backward Incompatible Changes

Enum cases are class constants, so this change affects them. Writing to or unsetting a readonly case property — for example unset(Enum::Case->value) or Enum::Case->value = $x — now raises the standard runtime readonly-property “Error” (“Cannot unset/modify readonly property”) instead of the previous compile-time fatal (“Cannot use temporary expression in write context”).

This makes enum-case property access consistent with plain variable access ($x = Enum::Case; unset($x->value);). Enum case properties remain immutable — no value that was previously read-only becomes writable, and dynamic properties on enum cases continue to be rejected.

Apart from this, code that previously terminated with a fatal error for CONST_OBJ->prop = ... will now execute successfully. This is a compatibility improvement — no previously-working code changes behavior.

Proposed PHP Version(s)

PHP 8.6

RFC Impact

To the Ecosystem

To Existing Extensions

No expected impact. This is a core compilation change around property write access, not an extension API change.

To SAPIs

No expected impact.

Open Issues

None currently.

Future Scope

Voting Choices

Primary Vote requiring a 2/3 majority to accept the RFC:

Accept the RFC Allow Object Property Writes on Objects Referenced by Constants?
Real name Yes No Abstain
alcaeus   
alexandredaubois   
bmajdak   
bwoebi   
crell   
daniels   
derick   
duncan3dc   
ericmann   
galvao   
jordikroon   
kalle   
kguest   
mbeccati   
nicolasgrekas   
ocramius   
ramsey   
sebastian   
sergey   
shivam   
svpernova09   
thekid   
theodorejb   
timwolla   
weilindu   
Final result: 17 2 6
This poll has been closed.

Patches and Tests

Proof of concept implementation:

Implementation

  1. Merged into: PHP 8.6

References

Rejected Features

Changelog

Key changes from v0.1: - Added by-ref passing examples (confirmed working in tests) - Added implementation details section explaining the compiler change - Added “Note on Array Constants” section addressing Ilija's and Tim's feedback about silent no-op dim writes - Expanded Future Scope with Tim's suggestion about warning on ASSIGN_DIM with non-pointer OP1 - Voting options were revised during later RFC updates; the current vote includes Yes, No, and Abstain - Removed the placeholder voting close date until voting opens - Targeted PHP 8.6 explicitly - Listed all test files in the Patches and Tests section - Removed Open Issues (the by-ref question is answered, isset/unset is confirmed) - Added full URLs to References - Added changelog entry for v0.2