From 1567ad2f880f11e41e202efb933f6805c4f65271 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 20 Apr 2026 17:07:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E8=A7=A3=E5=86=B3=E5=8C=BF?= =?UTF-8?q?=E5=90=8D=E7=B1=BB=E7=BB=A7=E6=89=BF=E7=88=B6=E7=B1=BB=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E8=A7=A3=E6=9E=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在编译器中为匿名类继承的父类添加全限定名称转换 - 修复了当匿名类继承使用use导入的类时的名称解析错误 - 添加了对继承类名的命名空间处理逻辑 - 确保匿名类能够正确继承并访问父类方法 --- src/Php/CompilerBase.php | 5 +++ .../001.phpt} | 0 tests/aot/anon_class/002.phpt | 35 +++++++++++++++++++ 3 files changed, 40 insertions(+) rename tests/aot/{anonymous-classes.phpt => anon_class/001.phpt} (100%) create mode 100644 tests/aot/anon_class/002.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 50e06312..f4d5ea3d 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3071,6 +3071,11 @@ class CompilerBase extends \PhpAot\Core\Translator $classDef = $expr->class; $className = $this->genAnonClassName(); $classDef->name = new Node\Identifier($className); + // 继承父类可能是 use 的名称,需要转换成全限定名称 + if ($classDef->extends !== null) { + $parentClass = $this->getNamespacedClassName($classDef->extends->toString()); + $classDef->extends = new Node\Name\FullyQualified($parentClass); + } $this->context->beforeStmtLines[] = 'static THREAD_LOCAL bool ' . $className . '_defined = false;'; $classCode = $this->genEmbeddedCode($classDef); $this->addConstData($className . '_code', $classCode); diff --git a/tests/aot/anonymous-classes.phpt b/tests/aot/anon_class/001.phpt similarity index 100% rename from tests/aot/anonymous-classes.phpt rename to tests/aot/anon_class/001.phpt diff --git a/tests/aot/anon_class/002.phpt b/tests/aot/anon_class/002.phpt new file mode 100644 index 00000000..fccb6324 --- /dev/null +++ b/tests/aot/anon_class/002.phpt @@ -0,0 +1,35 @@ +--TEST-- +Anonymous Classes - Runtime class definition +--FILE-- +greeting = $greeting; + } + + public function greet() { + return $this->greeting; + } + } +} + + +namespace { +use TestApp\Greeter; +function main() { + $class = new class("Hello") extends Greeter { + public function greet() { + return "{$this->greeting} from anonymous class"; + } + }; + echo $class->greet(), "\n"; +} +} + +?> +--EXPECT-- +Hello from anonymous class