feat(php): 添加字符串数组索引赋值支持

- 实现字符串类型的数组维度获取解析功能
- 添加字符串索引赋值的类型检测和错误处理
- 支持字符串的 offsetSet 操作转换
- 新增字符串数组索引赋值的功能测试用例
- 处理字符串索引为 null 时的错误情况
- 实现字符串变量类型检查机制
pull/1/head
韩天峰 7 months ago
parent 8257f8a1c4
commit 2d81a3975d
  1. 14
      src/Php/CompilerBase.php
  2. 12
      tests/aot/str-offset-set.phpt

@ -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) {

@ -0,0 +1,12 @@
--TEST--
strlen
--FILE--
<?php
$s1 = "hello world";
$s2 = "php";
$s1[1] = $s2[1] = '_';
var_dump($s1, $s2);
?>
--EXPECT--
string(11) "h_llo world"
string(3) "p_p"
Loading…
Cancel
Save