From f0c4b6bc987a6a5d1197d53d9edb4fb7566a0360 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 17 Jun 2026 16:27:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E8=A7=A3=E5=86=B3=E5=8F=AF?= =?UTF-8?q?=E9=80=89=E5=8F=82=E6=95=B0=E4=BC=A0=E5=85=A5null=E6=97=B6?= =?UTF-8?q?=E4=BD=BF=E7=94=A8C++=E9=BB=98=E8=AE=A4=E5=80=BC=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 当可选参数显式传入null时跳过该参数,使C++默认值生效 - 添加对substr、strpos、stripos、strrpos、strstr、explode等函数的null参数处理 - 确保null参数行为与PHP原生函数一致 - 新增测试用例验证各种可选参数为null时的正确行为 --- src/Php/Optimizer/FuncCallOptimizer.php | 11 ++++++ tests/aot/stdlib/null_optional_arg.phpt | 51 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/aot/stdlib/null_optional_arg.phpt diff --git a/src/Php/Optimizer/FuncCallOptimizer.php b/src/Php/Optimizer/FuncCallOptimizer.php index ef0d95c3..1407da95 100644 --- a/src/Php/Optimizer/FuncCallOptimizer.php +++ b/src/Php/Optimizer/FuncCallOptimizer.php @@ -393,6 +393,17 @@ trait FuncCallOptimizer } continue; } + // When null is explicitly passed to an optional parameter, skip it + // so the C++ default value takes effect (PHP null = "use default"). + if ($optional && $argCount > $i && isset($expr->args[$i])) { + $argVal = $expr->args[$i]->value; + if ($argVal instanceof Node\Expr\ConstFetch && strtolower($argVal->name->toString()) === 'null') { + if (isset($defaults[$i])) { + $args[] = $defaults[$i]; + } + continue; + } + } $args[] = $this->resolveArg($expr, $i, $type); } diff --git a/tests/aot/stdlib/null_optional_arg.phpt b/tests/aot/stdlib/null_optional_arg.phpt new file mode 100644 index 00000000..902320d9 --- /dev/null +++ b/tests/aot/stdlib/null_optional_arg.phpt @@ -0,0 +1,51 @@ +--TEST-- +Passing null to optional parameters should use C++ default values +--FILE-- + +--EXPECT-- +string(5) "world" +bool(true) +bool(true) +bool(true) +int(4) +bool(true) +int(4) +bool(true) +int(10) +bool(true) +string(7) "o world" +bool(true) +string(6) "ababab" +int(4)