From 84b93409987741a8fcd97247ed133d273cb2a71b Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 17 Apr 2026 20:04:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(aot):=20=E4=BF=AE=E5=A4=8DAOT=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E4=B8=AD=E7=B1=BB=E7=BB=A7=E6=89=BF=E5=92=8C=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=E5=AE=9A=E4=B9=89=E7=9B=B8=E5=85=B3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在WorkerA类中添加baz方法用于测试保护方法访问 - 修正isInheritedFrom方法中类名比较逻辑,确保正确的继承检查 - 更新对象实例类名获取方式,使用getFullClassName替代直接访问 - 修复常量定义解析中的多余换行符问题 - 扩展测试用例验证保护方法在不同实例间的调用行为 --- src/Php/CompilerBase.php | 4 ++-- src/Php/Translator.php | 2 +- tests/aot/ref/007.phpt | 12 +++++++++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 5d377ea3..50e06312 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4599,7 +4599,7 @@ class CompilerBase extends \PhpAot\Core\Translator } // 保护方法,只能当前类和子类使用 if ($flags & Modifiers::PROTECTED) { - return $this->isInheritedFrom($this->classDef->getNamespacedName(), $classDef->getNamespacedName()); + return $this->isInheritedFrom($this->classDef->getNamespacedName(false), $classDef->getNamespacedName(false)); } // 类外部调用,只允许调用 public 方法 return true; @@ -4615,7 +4615,7 @@ class CompilerBase extends \PhpAot\Core\Translator { $classDef = null; if ($object === 'this_') { - $class = $this->class; + $class = $this->getFullClassName(); $classDef = $this->classDef; } elseif (isset($this->context->objects[$object])) { $class = $this->context->objects[$object]; diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 165a26fb..9bb67887 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -980,7 +980,7 @@ class Translator extends Preprocessor $code .= $this->parseClass($v2); break; case 'Stmt_Const': - $this->parseConstDef($v2) . PHP_EOL; + $this->parseConstDef($v2); break; case 'Stmt_Function': $code .= $this->parseFunction($v2) . PHP_EOL; diff --git a/tests/aot/ref/007.phpt b/tests/aot/ref/007.phpt index b3f492b2..bc8f182c 100644 --- a/tests/aot/ref/007.phpt +++ b/tests/aot/ref/007.phpt @@ -7,6 +7,12 @@ class WorkerA protected function foo(string $name, string &$value) { $value = 'hello ' . $name; } + + public function baz(string $name) { + $value = ''; + $this->foo($name, $value); + return $value; + } } class WorkerB extends WorkerA @@ -22,7 +28,11 @@ function main() { $o = new WorkerB; var_dump($o->bar('php')); + + $o2 = new WorkerA; + var_dump($o2->baz('swoole')); } ?> --EXPECT-- -string(9) "hello php" \ No newline at end of file +string(9) "hello php" +string(12) "hello swoole" \ No newline at end of file