diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index b096cc4a..34a61df3 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -2755,7 +2755,7 @@ class CompilerBase extends \PhpAot\Core\Translator // 动态调用的函数,转换函数名为带有命名空间的全限定名称 $name = $this->getNamespacedFuncName($name); $code = $this->parseFuncCallWithOptimizer($name, $expr); - if ($code) { + if ($code !== false) { return $code; } $placeHolder = $this->identifierToStr($expr->name); diff --git a/src/Php/FuncCallOptimizer.php b/src/Php/FuncCallOptimizer.php index 54328e9d..cde7fb36 100644 --- a/src/Php/FuncCallOptimizer.php +++ b/src/Php/FuncCallOptimizer.php @@ -152,6 +152,9 @@ trait FuncCallOptimizer } } if ($name === 'strlen') { + if ($expr->args[0]->value instanceof Node\Scalar\String_) { + return strlen($expr->args[0]->value->value) . $this->getPlatform()->getIntegerLiteralSuffix(); + } return 'php::fn::strlen(' . $getArg(0) . ')'; } if ($name === 'ord') { diff --git a/tests/aot/optimizations/strlen-const.phpt b/tests/aot/optimizations/strlen-const.phpt new file mode 100644 index 00000000..23f6daca --- /dev/null +++ b/tests/aot/optimizations/strlen-const.phpt @@ -0,0 +1,18 @@ +--TEST-- +strlen(constant_string) compile-time folding +--FILE-- + +--EXPECT-- +int(5) +int(0) +int(11) +int(12) +int(2)