PHP 8.6 will let you change an object's properties through a constant. The Allow Object Property Writes on Objects Referenced by Constants RFC passed its vote and is now part of PHP 8.6.

PHP has allowed objects in constants since PHP 8.1. However, trying to change an object through the constant caused this fatal error: Cannot use temporary expression in write context.

That may sound like the constant was being changed, but it was not. The constant still pointed to the same object. Only a property on that object was changing.

What Changes in PHP 8.6

Code like this will work in PHP 8.6:

const OBJ = new stdClass();

OBJ->value = 123;
var_dump(OBJ->value); // int(123)

unset(OBJ->value);
var_dump(isset(OBJ->value)); // bool(false)

The change covers common property operations, including:

  • Setting a property
  • Increasing, decreasing, or adding to a value
  • Joining strings with .=
  • Using isset() or unset()
  • Changing a nested property
  • Passing a property by reference

For example, these operations are now valid:

const OBJ = new stdClass();

OBJ->counter = 0;
OBJ->counter++;
OBJ->counter += 10;

OBJ->message = 'hello';
OBJ->message .= ' world';

Passing a property by reference also works:

const OBJ = new stdClass();
OBJ->value = 10;

function update(&$value) {
    $value = 42;
}

update(OBJ->value);
var_dump(OBJ->value); // int(42)

Class Constants Work Too

The new behavior applies to both global constants and class constants. You can reach a class constant through the class name or an object:

const BACKING = new stdClass();

class Example {
    const OBJECT = BACKING;
}

Example::OBJECT->value = 42;

$example = new Example();
$example::OBJECT->value = 100;

var_dump(Example::OBJECT->value); // int(100)

Both lines change the same object.

Constants Are Still Immutable

This change does not let you replace the value stored in a constant. The constant must keep pointing to the same object:

const OBJ = new stdClass();

OBJ = new stdClass(); // Still illegal

Array and dimension writes are not included either:

const ITEMS = [1, 2, 3];
ITEMS[0] = 9; // Still rejected

const OBJ = new stdClass();
OBJ['key'] = 1; // Still rejected

Arrays use copy-on-write behavior, which makes them different from objects. The RFC leaves possible changes to array constants for a future proposal.

The Vote

The vote closed on August 8, 2026, with 17 votes in favor, two against, and six abstentions. It needed a two-thirds majority to pass.

The change was merged on August 13. You can review the code in php-src PR #20903. It also closes an issue reported in 2023.

One Small Compatibility Change

Most developers will only gain new working code from this change. There is one difference involving enum cases, which are also class constants.

Writing to an enum case's readonly property will still fail. However, PHP will now report the usual readonly-property error at runtime instead of the old temporary-expression error during compilation:

Enum::Case->value = $value;
unset(Enum::Case->value);

Enum properties do not become writable. Only the timing and message of the error change. Tests that check for the old error may need to be updated.

Static analysis tools may also need an update so they accept these new property writes.

Read More

Read the full RFC for the technical details. You can also catch up on PHP 8.6 Beta 1 and follow our other PHP 8.6 coverage.