From cc1131f45516cf99ceb90d4740a3dd1a9cd3ab38 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 7 Apr 2026 21:10:03 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E4=BC=98=E5=8C=96=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=8F=98=E9=87=8F=E8=AE=BF=E9=97=AE=E5=92=8C=E5=9F=BA?= =?UTF-8?q?=E5=87=86=E6=B5=8B=E8=AF=95=E5=87=BD=E6=95=B0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在CompilerBase.php中添加标量字符串维度检查,直接返回全局变量名称而不是调用php::global - 为micro_bench.php中的read_global_var、read_hash、read_str_offset和issetor函数添加int类型声明 - 改进全局变量处理逻辑,避免不必要的php::global调用 - 提升代码类型安全性和性能表现 --- examples/micro_bench.php | 8 ++++---- src/Php/CompilerBase.php | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/micro_bench.php b/examples/micro_bench.php index 3ccf990c..50be6743 100644 --- a/examples/micro_bench.php +++ b/examples/micro_bench.php @@ -188,27 +188,27 @@ function read_auto_global($n) { } } -function read_global_var($n) { +function read_global_var(int $n) { for ($i = 0; $i < $n; ++$i) { $x = $GLOBALS['g_var']; } } -function read_hash($n) { +function read_hash(int $n) { $hash = array('test' => 0); for ($i = 0; $i < $n; ++$i) { $x = $hash['test']; } } -function read_str_offset($n) { +function read_str_offset(int $n) { $str = "test"; for ($i = 0; $i < $n; ++$i) { $x = $str[1]; } } -function issetor($n) { +function issetor(int $n) { $val = array(0,1,2,3,4,5,6,7,8,9); for ($i = 0; $i < $n; ++$i) { $x = $val ?: null; diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index a49b6e2b..67ac7fd5 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -2284,6 +2284,13 @@ class CompilerBase extends \PhpAot\Core\Translator if ($node->dim === null) { $this->fatalError($node, 'Cannot use [] for GLOBALS'); } + if ($this->isScalarString($node->dim)) { + $name = $node->dim->value; + if (!$this->hasGlobalVar($name)) { + $this->addGlobalVar($name, self::TYPE_VAR); + } + return $name; + } return 'php::global(' . $this->parseIdentifier($node->dim) . ')'; } if (!$this->hasVar($var)) {