From 8deb99c372cb64bd71217379a08a4ab8b91dc1bd Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 2 Jun 2026 11:28:15 +0800 Subject: [PATCH] =?UTF-8?q?test(aot):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E5=B1=9E=E6=80=A7toArray=E6=96=B9=E6=B3=95=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 测试toArray()方法调度功能 - 验证(array)强制转换调用toArray()而不是读取属性表 - 添加User类包含toArray方法的测试 - 添加PlainUser类无toArray方法的对照测试 - 验证有toArray方法时强制转换调用该方法 - 验证无toArray方法时回退到属性表读取 --- tests/aot/object_property/toarray-method.phpt | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/aot/object_property/toarray-method.phpt diff --git a/tests/aot/object_property/toarray-method.phpt b/tests/aot/object_property/toarray-method.phpt new file mode 100644 index 00000000..0080c9d0 --- /dev/null +++ b/tests/aot/object_property/toarray-method.phpt @@ -0,0 +1,55 @@ +--TEST-- +toArray() method dispatch: (array) cast calls toArray() instead of reading property table +--FILE-- +id = $id; + $this->name = $name; + } + + public function toArray(): array { + return [ + 'uid' => $this->id, + 'display_name' => $this->name, + 'source' => 'toArray', + ]; + } +} + +class PlainUser { + public int $id; + public string $name; + + public function __construct(int $id, string $name) { + $this->id = $id; + $this->name = $name; + } +} + +function main() { + $user = new User(1, 'admin'); + // (array) cast should call User::toArray(), not read properties + $arr = (array) $user; + var_dump($arr['uid']); + var_dump($arr['display_name']); + var_dump($arr['source']); + + // no toArray() method — falls back to property table + $plain = new PlainUser(2, 'guest'); + $arr2 = (array) $plain; + var_dump($arr2['id']); + var_dump($arr2['name']); +} + +?> +--EXPECT-- +int(1) +string(5) "admin" +string(7) "toArray" +int(2) +string(5) "guest"