From 2d81a3975df916316d1321fa63e2c348583a4e82 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 4 Feb 2026 17:54:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E6=95=B0=E7=BB=84=E7=B4=A2=E5=BC=95=E8=B5=8B?= =?UTF-8?q?=E5=80=BC=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现字符串类型的数组维度获取解析功能 - 添加字符串索引赋值的类型检测和错误处理 - 支持字符串的 offsetSet 操作转换 - 新增字符串数组索引赋值的功能测试用例 - 处理字符串索引为 null 时的错误情况 - 实现字符串变量类型检查机制 --- src/Php/CompilerBase.php | 14 ++++++++++++++ tests/aot/str-offset-set.phpt | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 tests/aot/str-offset-set.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index dc0774c8..2cb10054 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1079,6 +1079,15 @@ class CompilerBase extends \PhpAot\Core\Translator } } elseif ($this->isPropertyFetch($left)) { $var = $this->parsePropertyFetch($left, true); + } elseif ($this->isArrayDimFetch($left)) { + $tmp = $this->parseIdentifier($left->var); + var_dump($tmp); + if ($this->isVarExpr($left->var) and $this->getVarType($tmp) === self::TYPE_STR) { + if ($left->dim === null) { + $this->fatalError($left, 'Cannot use [] for strings'); + } + return $tmp . '.offsetSet(' . $this->parseExpr($left->dim) . ', ' . $this->convertExprType($expr, $this->detectExprType($left), $this->detectExprType($right)) . ')'; + } } return $var . ' = ' . $this->convertExprType($expr, $this->detectExprType($left), $this->detectExprType($right)); @@ -1689,6 +1698,11 @@ class CompilerBase extends \PhpAot\Core\Translator $this->fatalError($node->var, "The variable `{$node->var->name}` is undefined"); } } + if ($this->getVarType($var) === self::TYPE_STR) { + if ($node->dim === null) { + $this->fatalError($node, 'Cannot use [] for strings'); + } + } } if ($node->dim === null) { diff --git a/tests/aot/str-offset-set.phpt b/tests/aot/str-offset-set.phpt new file mode 100644 index 00000000..6954557c --- /dev/null +++ b/tests/aot/str-offset-set.phpt @@ -0,0 +1,12 @@ +--TEST-- +strlen +--FILE-- + +--EXPECT-- +string(11) "h_llo world" +string(3) "p_p"