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"