Skip to content

Commit defd8b2

Browse files
committed
Fix iteration of data class by-ref
1 parent e7f1d8d commit defd8b2

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Zend/tests/data_classes/iter.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Iteration of data classes
3+
--FILE--
4+
<?php
5+
6+
#[AllowDynamicProperties]
7+
data class Box {
8+
public $value;
9+
}
10+
11+
$box = new Box();
12+
$box->value = 1;
13+
$copy = $box;
14+
15+
foreach ($box as $value) {
16+
var_dump($value);
17+
}
18+
19+
foreach ($box as $prop => &$value) {
20+
if ($box->value === 1) {
21+
$box->dynamic = 1;
22+
}
23+
$value++;
24+
var_dump($prop, $value);
25+
}
26+
27+
?>
28+
--EXPECT--
29+
int(1)
30+
string(5) "value"
31+
int(2)
32+
string(7) "dynamic"
33+
int(2)

Zend/zend_vm_def.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6884,6 +6884,9 @@ ZEND_VM_COLD_CONST_HANDLER(125, ZEND_FE_RESET_RW, CONST|TMP|VAR|CV, JMP_ADDR)
68846884
ZEND_VM_NEXT_OPCODE();
68856885
} else if (OP1_TYPE != IS_CONST && EXPECTED(Z_TYPE_P(array_ptr) == IS_OBJECT)) {
68866886
if (!Z_OBJCE_P(array_ptr)->get_iterator) {
6887+
if (OP1_TYPE & (IS_CV|IS_VAR)) {
6888+
SEPARATE_DATA_OBJ(array_ptr);
6889+
}
68876890
HashTable *properties;
68886891
if (OP1_TYPE == IS_VAR || OP1_TYPE == IS_CV) {
68896892
if (array_ptr == array_ref) {

Zend/zend_vm_execute.h

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)