From a54985897fbdc7a9271487a32ac4ba71da29a022 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 28 Feb 2026 19:24:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E7=B1=BB?= =?UTF-8?q?=E5=90=8D=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=92=8C=E9=9D=99?= =?UTF-8?q?=E6=80=81=E5=B1=9E=E6=80=A7=E5=88=97=E8=A1=A8=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 getNativeName 方法中函数名为空时的处理逻辑 - 添加调试追踪用于非Test类的原生类检查 - 使用 getNamespacedClassName 替代直接获取类名进行查找 - 修改静态属性列表为关联数组以支持完整的属性标识符 --- src/Php/CompilerBase.php | 17 ++++++++++++----- src/Php/Translator.php | 10 +++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 36ab3cbe..2bd65923 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -643,13 +643,16 @@ class CompilerBase extends \PhpAot\Core\Translator protected function getNativeName(string $fn, string $ns = '', string $class = ''): string { + $names = []; if ($ns) { $names[] = $this->escapeNamespace($ns); } if ($class) { $names[] = $this->escapeClass($class); } - $names[] = $this->escapeName($fn); + if ($fn) { + $names[] = $this->escapeName($fn); + } return implode(self::NAMESPACE_SEPARATOR, $names); } @@ -1452,6 +1455,9 @@ class CompilerBase extends \PhpAot\Core\Translator */ protected function hasNativeClass(string $name): bool { + if (!str_starts_with($name, 'Test')) { + debug_print_backtrace(); + } return array_key_exists($this->escapeClass($name), $this->classes); } @@ -3360,7 +3366,7 @@ class CompilerBase extends \PhpAot\Core\Translator return $id; } if ($id === 'self') { - $id = $this->classDef->getNamespacedName(false); + $id = $this->getNamespacedClassName($this->class); } if ($this->isNameExpr($node) or $this->isIdExpr($node)) { return $this->genCharPtr($id, true); @@ -3441,11 +3447,12 @@ class CompilerBase extends \PhpAot\Core\Translator $class = $this->class; } - if (!$this->hasNativeClass($class)) { + $fullName = $this->getNamespacedClassName($class); + if (!$this->hasNativeClass($fullName)) { return null; } - $classDef = $this->classes[$class]; + $classDef = $this->getClassDef($fullName); $namespace = $classDef->namespace; if ($classDef->hasProperty($prop)) { $propDef = $classDef->getProperty($prop); @@ -3500,7 +3507,7 @@ class CompilerBase extends \PhpAot\Core\Translator { $nativeProp = $this->findNativeStaticProperty($expr, $class, $namespace); if ($nativeProp) { - $classPtr = $this->getClassEntryPtr($class); + $classPtr = $this->getClassEntryPtr($this->getNamespacedClassName($class)); $propOffset = self::PREFIX . $this->getPropertyOffset($nativeProp->name, $class, $namespace); return 'php::getStaticProperty(' . $classPtr . ', ' . $propOffset . ')'; } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 3eaf752f..edebf6a8 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -690,12 +690,12 @@ class Translator extends Preprocessor $extends = $class->extends; } - $nativeName = $this->getNativeName('', $this->class, $this->namespace); - if ($this->hasNativeClass($nativeName)) { - $this->classDef = $this->classes[$nativeName]; + $fullName = $this->getNamespacedClassName($this->class); + if ($this->hasNativeClass($fullName)) { + $this->classDef = $this->classes[$fullName]; } else { $this->classDef = new ClassDef($this->class, $flags, $this->namespace); - $this->addClass($nativeName, $this->classDef); + $this->addClass($fullName, $this->classDef); } if ($class instanceof Node\Stmt\Enum_) { @@ -817,7 +817,7 @@ class Translator extends Preprocessor $prop->class = $classDef->getNamespacedName(false); $prop->name = $property->name; $prop->default = $property->default; - $this->staticPropertyList[] = $prop; + $this->staticPropertyList[$name . '::' . $property->name] = $prop; } else { $propOffset = self::PREFIX . $this->getPropertyOffset($property->name, $classDef->name, $classDef->namespace); $cppCode .= $this->getIndent() . 'this_.attr(' . $propOffset . ') = ' . $property->default . ';' . PHP_EOL;