From 492a1dbd391aaa87734ceed9b96f4df9b4b0050d Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 17 Apr 2026 19:41:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E7=B1=BB?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=AE=BF=E9=97=AE=E6=9D=83=E9=99=90=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构 checkAccessible 方法,正确处理 private 和 protected 方法的访问控制 - 添加对受保护方法的继承关系检查 - 修复当前类内部访问权限判断逻辑 - 优化 findNativeMethod 中的对象方法查找流程 - 添加缺失的返回值处理逻辑 --- src/Php/CompilerBase.php | 32 +++++++++++++++++++------------- tests/aot/ref/007.phpt | 28 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 tests/aot/ref/007.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 222b8e31..5d377ea3 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4593,13 +4593,16 @@ class CompilerBase extends \PhpAot\Core\Translator protected function checkAccessible(ClassDef $classDef, int $flags): bool { - // 在当前类中,允许调用所有方法 - if ($classDef->namespace === $this->namespace and $classDef->name == $this->class) { - return true; + // 私有方法,只能当前的类使用 + if ($flags & Modifiers::PRIVATE) { + return $classDef->namespace === $this->namespace and $classDef->name == $this->class; + } + // 保护方法,只能当前类和子类使用 + if ($flags & Modifiers::PROTECTED) { + return $this->isInheritedFrom($this->classDef->getNamespacedName(), $classDef->getNamespacedName()); } - // 类外部调用,只允许调用 public 方法 - return $flags & Modifiers::PUBLIC; + return true; } protected function isOverrideMethod(string $fullMethodName): bool @@ -4610,21 +4613,24 @@ class CompilerBase extends \PhpAot\Core\Translator protected function findNativeMethod(CallLike $expr, string $object, string $method): string|false { - $nativeFunc = ''; $classDef = null; if ($object === 'this_') { - $nativeFunc = $this->getNativeName($method, $this->namespace, $this->class); + $class = $this->class; $classDef = $this->classDef; } elseif (isset($this->context->objects[$object])) { $class = $this->context->objects[$object]; - $nativeFunc = $this->getNativeMethod($expr, $class, $method); - // 存在 Native 类,但是没有找到方法,可能是动态调用 - if (!$nativeFunc) { - if ($this->hasClass($class) and $this->getNativeMethod($expr, $class, '__call', false)) { - throw new DynamicCall(); - } + } else { + return false; + } + + $nativeFunc = $this->getNativeMethod($expr, $class, $method); + // 存在 Native 类,但是没有找到方法,可能是动态调用 + if (!$nativeFunc) { + if ($this->hasClass($class) and $this->getNativeMethod($expr, $class, '__call', false)) { + throw new DynamicCall(); } } + if ($classDef) { $fullMethodName = $classDef->getNamespacedName(false) . '::' . $method; } else { diff --git a/tests/aot/ref/007.phpt b/tests/aot/ref/007.phpt new file mode 100644 index 00000000..b3f492b2 --- /dev/null +++ b/tests/aot/ref/007.phpt @@ -0,0 +1,28 @@ +--TEST-- +class const 001 +--FILE-- +foo($name, $value); + return $value; + } +} + +function main() +{ + $o = new WorkerB; + var_dump($o->bar('php')); +} +?> +--EXPECT-- +string(9) "hello php" \ No newline at end of file