forward_static_call_array
Calls a callable with an array of arguments while preserving the current called class.
forward_static_call_array(callable $callback, array $args): mixed- Added in PHP 5.3.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$callback | callable | The function or method to call. May be a ['Class', 'method'] array, a 'Class::method' string, or a function name. |
$args | array | The arguments for the callback. To pass a parameter by reference, the corresponding array element must itself be a reference. |
Return value
mixedThe value returned by the callback, or false on error.
Errors
Throws a TypeError if callback is not callable, and an Error if a string key in args does not match a parameter name.
Changelog
- PHP 8.0String keys in args are interpreted as named arguments, matching the change made to call_user_func_array().
This is forward_static_call() with the argument list supplied as an array. It makes a forwarding call, meaning static:: inside the target method still resolves to the class the original call was made against instead of being reset to the class named in the callback.
Where it earns its place
The classic case is a __callStatic() handler in a base class. That magic method receives the arguments as an array, and if it dispatches to a real static method you almost always want late static binding preserved so the subclass, not the base, is what static::class reports. The same applies to macro or mixin systems that register static handlers and replay them later.
One important difference from forward_static_call
This function does not require an active class scope. Calling it from a plain function or the global scope is legal; it simply behaves like call_user_func_array() because there is no called class to forward. That makes it the safer of the two when the calling context is not guaranteed.
As with call_user_func_array(), since PHP 8.0 string keys in the argument array are named arguments, not ignored labels.
Example
When to use it
- Dispatching from __callStatic() to a real static method without losing the subclass identity
- Implementing a macro or mixin registry where static handlers are replayed with recorded arguments
- Forwarding a variadic static wrapper to a parent implementation chosen at runtime
Frequently asked questions
What is the difference between forward_static_call_array and call_user_func_array?
forward_static_call_array() preserves the current late static binding class, so static:: inside the callback resolves to the class the original call was made against. call_user_func_array() resets it to the class named in the callback.
Does forward_static_call_array work outside a class?
Yes. Unlike forward_static_call(), it does not throw when there is no class scope; it simply makes an ordinary non-forwarding call because there is nothing to forward.
When should I use this instead of the spread operator?
Use $callback(...$args) whenever you do not need late static binding preserved. Only this function keeps static:: pointing at the original called class when the callback is written as a Class/method pair.
More examples
Forwarding to a parent with a built argument list
Prints 'UserQuery: SELECT id, email FROM users'. The parent method reads static::class and still sees UserQuery because the call was forwarded, even though the arguments were assembled into an array first.
<?php
class Query
{
public static function build(string ...$parts): string
{
return static::class . ': ' . implode(' ', $parts);
}
}
class UserQuery extends Query
{
public static function build(string ...$parts): string
{
$args = array_merge(['SELECT'], $parts, ['FROM users']);
return forward_static_call_array(['parent', 'build'], $args);
}
}
echo UserQuery::build('id,', 'email'), "\n";No class scope required
Prints Sharp and then the confirmation line. forward_static_call() would have thrown an Error here; this variant runs, but with nothing to forward it behaves exactly like call_user_func_array().
<?php
class Tool { public static function name(): string { return static::class; } }
class Sharp extends Tool {}
function fromGlobalScope(): string
{
return forward_static_call_array(['Sharp', 'name'], []);
}
echo fromGlobalScope(), "\n";
echo "no Error thrown\n";Named arguments in the array
Prints 'Monthly -> sales.csv'. The string keys are matched to parameter names rather than positions, which is why the out-of-order array still works on PHP 8. On PHP 7 the same array would have been passed positionally.
<?php
class Report
{
public static function render(string $title, string $format = 'txt'): string
{
return static::class . " -> $title.$format";
}
}
class Monthly extends Report
{
public static function go(): string
{
return forward_static_call_array(['Report', 'render'], ['format' => 'csv', 'title' => 'sales']);
}
}
echo Monthly::go(), "\n";
Discussion