Skip to content

Fix GH-10497: Allow const obj->prop = value#20903

Open
khaledalam wants to merge 3 commits intophp:masterfrom
khaledalam:fix-gh10497-allow-const-object-modification
Open

Fix GH-10497: Allow const obj->prop = value#20903
khaledalam wants to merge 3 commits intophp:masterfrom
khaledalam:fix-gh10497-allow-const-object-modification

Conversation

@khaledalam
Copy link
Copy Markdown
Contributor

Description

Fixes #10497 - Allows direct modification of object properties stored in constants.

Problem

Previously, this code would fail with a fatal error:

const a = new stdClass;
a->b = 42;  // Fatal error: Cannot use temporary expression in write context

Solution

Modified the compiler to properly handle ZEND_AST_CONST in variable compilation contexts:

Constants now emit IS_VAR instead of IS_TMP_VAR when used in write contexts
This allows property assignment opcodes to work correctly with constant objects

Tests:

  • Added Zend/tests/gh10497.phpt - comprehensive test for the fix
  • Updated Zend/tests/gh12102_3.phpt - reflects improved constant handling

Behavior

Now supports all property operations on constant objects:

const obj = new stdClass;
obj->prop = 'value';
obj->prop = 'new value';
obj->another = 'test';
obj->counter = 0;
obj->counter++;
obj->num = 10;
obj->num += 5;
unset(obj->prop);
obj->nested = new stdClass;
obj->nested->value = 123;

The constant reference remains immutable, but the object itself is mutable as expected.

Copy link
Copy Markdown
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @khaledalam! Looks reasonable overall.

@khaledalam
Copy link
Copy Markdown
Contributor Author

Thanks @khaledalam! Looks reasonable overall.

Thanks @iluuu1994 for the feedback, all comments addressed.

@iluuu1994
Copy link
Copy Markdown
Member

Cool, thanks @khaledalam! This is a language change and should be discussed on the internals mailing list at least. I think this will impact GH-13800, but that's not a major issue.

@iluuu1994
Copy link
Copy Markdown
Member

Btw, one thing in particular people might object to is:

const arrayTest = [1, 2, 3];
arrayTest[] = 4;
var_dump(arrayTest);

This becomes valid, but prints:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

I.e. the assignment has no effect, because it happens on a temporary value. Make sure to mention this in your e-mail to internals.

@khaledalam
Copy link
Copy Markdown
Contributor Author

I've opened an internals thread and sent an email for discussion: https://news-web.php.net/php.internals/129757

@khaledalam khaledalam requested a review from iluuu1994 January 31, 2026 00:03
@iluuu1994
Copy link
Copy Markdown
Member

It sounds like @TimWolla objected, so this would need an RFC, or at least the mitigation from https://news-web.php.net/php.internals/129839.

Copy link
Copy Markdown
Member

@TimWolla TimWolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my replies on the list were an objection against the PR as-is. Selecting request changes for visibility.

@khaledalam
Copy link
Copy Markdown
Contributor Author

Noted, I'll look into it

Pass type parameter to zend_compile_const() instead of bool use_tmp
Check BP_VAR_R/BP_VAR_IS for read mode
Remove unnecessary wrapper function
Return void instead of zend_op*
@khaledalam khaledalam force-pushed the fix-gh10497-allow-const-object-modification branch from d488d10 to b0a2437 Compare April 3, 2026 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot directly modify an object stored in a constant

3 participants