diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 8e453aec..ca871ef0 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -892,7 +892,7 @@ class CompilerBase extends \PhpAot\Core\Translator return strtolower($fullClassName . '::' . $method); } - public function getNamespacedClassName(string $class): string + public function getNamespacedClassName(string $class, string $currentNamespace = ''): string { if ($class === '') { $this->error('Class name can not be empty'); @@ -920,7 +920,9 @@ class CompilerBase extends \PhpAot\Core\Translator } } - $currentNamespace = $this->namespace; + if (!$currentNamespace) { + $currentNamespace = $this->namespace; + } if (!empty($currentNamespace)) { return trim($currentNamespace, '\\') . '\\' . $class; } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 96484f7a..e608ba82 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -1195,9 +1195,13 @@ CODE; * 仅用于 gen_stub 脚本 * @throws \Exception */ - public function getClassConstValue(NodeAbstract $expr, string $class, string $name): mixed + public function getClassConstValue(NodeAbstract $expr, string $_class, string $name, string $currentClass): mixed { - $class = $this->getNamespacedClassName($class); + $namespace = $this->namespace; + if (!$namespace) { + $namespace = (string)strstr($currentClass, '\\', true); + } + $class = $this->getNamespacedClassName($_class, $namespace); $nativeConst = $this->findNativeClassConst($expr, $class, $name); if ($nativeConst and $expr->hasAttribute('nativeConst')) { $constDef = $expr->getAttribute('nativeConst'); diff --git a/src/gen_stub.php b/src/gen_stub.php index 30fb1f7a..722b6eb8 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -2309,12 +2309,12 @@ class EvaluatedValue if (isset($allConstInfos[$constName])) { return $allConstInfos[$constName]->getValue($allConstInfos)->value; } else { - return getTranslator()->getClassConstValue($expr, ClassInfo::$currentClass, $expr->name->toString()); + return getTranslator()->getClassConstValue($expr, ClassInfo::$currentClass, $expr->name->toString(), ClassInfo::$currentClass); } } elseif ($expr->name->__toString() === 'class') { return $class; } else { - return getTranslator()->getClassConstValue($expr, $class, $expr->name->__toString()); + return getTranslator()->getClassConstValue($expr, $class, $expr->name->__toString(), ClassInfo::$currentClass); } } else { $constName = $expr->name->__toString(); @@ -4525,6 +4525,8 @@ class FileInfo { $this->getMinimumPhpVersionIdCompatibility(), $this->isUndocumentable ); + // 清理当前类名,避免污染 + ClassInfo::$currentClass = ''; continue; } @@ -4582,6 +4584,7 @@ class FileInfo { foreach ($this->classInfos as $class) { ClassInfo::$currentClass = $class->name->toString(); $code .= "\n" . $class->getRegistration($allConstInfos); + ClassInfo::$currentClass = ''; } return $code; @@ -4866,7 +4869,7 @@ function parseFunctionLike( } if ($param->default instanceof Expr\ClassConstFetch && $param->default->class->toLowerString() === "self") { - $defaultValue = getTranslator()->getClassConstValue($func, $name->className->name, $param->default->name->name); + $defaultValue = getTranslator()->getClassConstValue($func, $name->className->name, $param->default->name->name, ClassInfo::$currentClass); } else { $defaultValue = $param->default ? $prettyPrinter->prettyPrintExpr($param->default) : null; } diff --git a/tests/aot/trait/011.phpt b/tests/aot/trait/011.phpt new file mode 100644 index 00000000..1b44be0c --- /dev/null +++ b/tests/aot/trait/011.phpt @@ -0,0 +1,38 @@ +--TEST-- +trait full class name +--FILE-- + 'Hello 1', + ]; + public function hello() + { + var_dump(self::DATA); + var_dump(self::DATA[TraitsTest::NAME]); + } + } + + class TraitsTest + { + public const NAME = 'test_trait'; + use THello1; + } +} + +namespace { + function main() + { + $o = new App2\TraitsTest; + $o->hello(); + } +} +?> +--EXPECT-- +array(1) { + ["test_trait"]=> + string(7) "Hello 1" +} +string(7) "Hello 1" \ No newline at end of file