From a1d74df9853e3f3e5abd978d2e897591749c2018 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 11 Feb 2026 15:02:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(compiler):=20=E6=94=AF=E6=8C=81=E5=8C=BF?= =?UTF-8?q?=E5=90=8D=E7=B1=BB=E9=9D=99=E6=80=81=E8=AE=BF=E9=97=AE=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=92=8C=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加匿名类索引计数器用于生成唯一类名 - 实现对象类型获取功能以支持动态类型解析 - 优化静态调用解析逻辑以处理变量表达式情况 - 添加测试用例验证匿名类静态访问功能 --- src/Php/CompilerBase.php | 21 ++++++++++++++++++--- tests/zend/anon/008.phpt | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/zend/anon/008.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 1b16db49..812ab7de 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -73,6 +73,7 @@ class CompilerBase extends \PhpAot\Core\Translator protected array $literalStrings = []; protected int $literalStringIndex = 0; protected int $tmpVarIndex = 0; + protected int $anonClassIndex = 0; protected int $classIndex = 0; /** @@ -252,6 +253,11 @@ class CompilerBase extends \PhpAot\Core\Translator return isset($this->objects[$object]); } + public function getObjectType(string $object): string + { + return $this->objects[$object] ?? 'stdClass'; + } + public function parseExpr(mixed $expr) { $type = $expr->getType(); @@ -485,7 +491,7 @@ class CompilerBase extends \PhpAot\Core\Translator public function genAnonClassName(): string { - return self::ANON_CLASS . $this->tmpVarIndex++; + return self::ANON_CLASS . $this->anonClassIndex++; } public function writeFile(string $file, string $content): void @@ -3156,16 +3162,25 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseStaticCall(Node\Expr\StaticCall $expr): string { if ($this->isVarExpr($expr->class) or $this->isVarExpr($expr->name)) { - $fn = 'php::concat({' . $this->identifierToStr($expr->class) . ', "::", ' . $this->identifierToStr($expr->name) . '})'; + $var = $this->parseIdentifier($expr->class); + if ($this->isTypedObject($var)) { + $class = $this->getObjectType($var); + goto _do_call; + } elseif ($this->getVarType($var) == self::TYPE_OBJECT) { + $fn = 'php::concat({' . $var . '.getClassName()' . ', "::", ' . $this->identifierToStr($expr->name) . '})'; + } else { + $fn = 'php::concat({' . $this->identifierToStr($expr->class) . ', "::", ' . $this->identifierToStr($expr->name) . '})'; + } } else { $class = $this->parseIdentifier($expr->class); - $method = $this->parseIdentifier($expr->name); if ($class === 'self') { $class = $this->class; } elseif ($class === 'parent') { return $this->parseParentMethodCall($expr); } + _do_call: + $method = $this->parseIdentifier($expr->name); $this->beforeStmtLines[] = '// Static Call: ' . $class . '::' . $method; if ($this->isNameExpr($expr->class) and $this->isIdExpr($expr->name)) { diff --git a/tests/zend/anon/008.phpt b/tests/zend/anon/008.phpt new file mode 100644 index 00000000..2b0ca3d7 --- /dev/null +++ b/tests/zend/anon/008.phpt @@ -0,0 +1,25 @@ +--TEST-- +testing static access for methods and properties in anon classes +--FILE-- + +--EXPECT-- +string(4) "cats" +string(4) "dogs"