Assigning property value through readonly array results in "Cannot modify readonly property"
还没有人认领这个 Issue。
评估
调研方向
首先在 PHP 8.2.23 和 8.3.11 上运行提供的复现程序,并将其与通过临时变量进行重构后的访问方式进行比较。在 PHP 解释器中跟踪直接的 $a->[0]->b 表达式的 readonly 属性处理;当直接赋值更新 B::$b 并在没有 fatal error 的情况下打印 bar 时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Description
The following code:
<?php
class A {
public function __construct(
public readonly array $a,
) {}
}
class B {
public function __construct(
public string $b,
) {
}
}
$a = new A([ new B('foo') ]);
$a->a[0]->b = 'bar';
echo $a->a[0]->b;
Resulted in this output:
PHP Fatal error: Uncaught Error: Cannot modify readonly property A::$a in /test.php:17
But I expected this output instead:
bar
If you try to change the property of an object that is inside a readonly array, based on syntax it thinks you're change the array itself and throws a Fatal, even though that property we're actually trying to change ($a->a[0]->b) is not readonly and can be changed. If the code to change the B::$b property is refactored like this the code works as expected:
$a = new A([ new B('foo') ]);
$b = $a->a[0];
$b->b = 'bar';
PHP Version
8.2.23, 8.3.11
Operating System
No response
- 主要语言
- C
- 星标
- 40.4k
- 派生
- 8.2k
- 平均合并
- 2 天 15 小时
- 30 天内合并 PR
- 103
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
php/php-src 的其他 Issue
-
Bug SAPI: cli_server Status: Verified
难度 2/5 1-3 小时 新手友好度 88/100
-
Bug Status: Needs Triage
难度 2/5 1-3 小时 新手友好度 76/100
-
Bug Status: Needs Triage
难度 1/5 1 小时以内 新手友好度 90/100
-
Bug Status: Needs Triage
难度 2/5 1-3 小时 新手友好度 78/100
-
Bug Category: Tests Status: Verified
难度 2/5 1-3 小时 新手友好度 68/100
相似的 Issue
-
[adam] AdamNet network read doesn't cap to MAX_ADAM_PACKET_LEN, overflows client receive buffers 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
FujiNetWIFI/fujinet-firmware#1649 · 2 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
HarbourMasters/Shipwright#7229 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
riscv-software-src/riscv-isa-sim#2435 · 1 条评论 ·
-
bug Self Built Image SNAPSHOT Supported Device target/ramips
难度 2/5 1-3 小时 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 76/100