From 30ef9352a245f5c9052cc832c33c2ea39f0c7ba3 Mon Sep 17 00:00:00 2001 From: Yurun Date: Wed, 17 Jun 2026 19:52:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(const):=20=E4=BF=AE=E5=A4=8D=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=E5=90=8D=E4=B8=8E=E5=8F=98=E9=87=8F=E5=90=8D=E5=86=B2?= =?UTF-8?q?=E7=AA=81=E9=97=AE=E9=A2=98=E3=80=81=E5=B8=B8=E9=87=8F=E5=90=8D?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E5=86=99=E6=95=8F=E6=84=9F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Php/CompilerBase.php | 9 +++++---- src/Php/Generator/Utils.php | 5 +++++ tests/aot/const/const-same-name.phpt | 15 +++++++++++++++ tests/aot/const/const-var-same-name.phpt | 15 +++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 tests/aot/const/const-same-name.phpt create mode 100644 tests/aot/const/const-var-same-name.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 91c678e4..569da39b 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -143,6 +143,7 @@ class CompilerBase extends \PhpAot\Core\Translator public const string DYNAMIC_CALLED_CLASS = '__dynamic_called_class__'; public const string STATIC_VAR = '_static_var_'; public const string GLOBAL_VAR = '_global_var_'; + public const string CONST_VAR = '_const_var_'; public const string OBJECT_PROP = '_object_prop_'; public const string CLASS_MAP = 'class_map'; public const string FUNC_MAP = 'func_map'; @@ -5021,22 +5022,22 @@ class CompilerBase extends \PhpAot\Core\Translator $constInfo->type = $this->detectStrValueType($value); $constInfo->namespace = $this->namespace; $constInfo->name = $name; - $this->constants[$this->escapeNamespace($name)] = $constInfo; + $this->constants[$this->escapeConstVar($name)] = $constInfo; } protected function hasConstant(string $name): bool { - return isset($this->constants[$this->escapeNamespace($name)]); + return isset($this->constants[$this->escapeConstVar($name)]); } protected function getConstant(string $name): string { - return $this->escapeNamespace($name); + return $this->escapeConstVar($name); } protected function getConstantType(string $name): string { - return $this->constants[$this->escapeNamespace($name)]->type; + return $this->constants[$this->escapeConstVar($name)]->type; } protected function detectStrValueType(mixed $constant): string diff --git a/src/Php/Generator/Utils.php b/src/Php/Generator/Utils.php index 89391995..284bcf8f 100644 --- a/src/Php/Generator/Utils.php +++ b/src/Php/Generator/Utils.php @@ -95,6 +95,11 @@ trait Utils return self::GLOBAL_VAR . $name; } + protected function escapeConstVar(string $name): string + { + return self::CONST_VAR . str_replace('\\', self::NAMESPACE_SEPARATOR, $name); + } + protected function escapeNamespace(string $ns): string { return str_replace('\\', self::NAMESPACE_SEPARATOR, strtolower($ns)); diff --git a/tests/aot/const/const-same-name.phpt b/tests/aot/const/const-same-name.phpt new file mode 100644 index 00000000..8a024fa3 --- /dev/null +++ b/tests/aot/const/const-same-name.phpt @@ -0,0 +1,15 @@ +--TEST-- +Constant with the same name (case-insensitive) resolve independently +--FILE-- + +--EXPECT-- +int(123) +int(456) diff --git a/tests/aot/const/const-var-same-name.phpt b/tests/aot/const/const-var-same-name.phpt new file mode 100644 index 00000000..28bf1a9c --- /dev/null +++ b/tests/aot/const/const-var-same-name.phpt @@ -0,0 +1,15 @@ +--TEST-- +Constant and variable with the same name (case-insensitive) resolve independently +--FILE-- + +--EXPECT-- +int(123) +int(456)