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)