PHP late static binding and forward_static_call()
phplate-static-bindingstaticoopcallables
In PHP, self:: refers to the class where the code is written, while static:: refers to the called class (late static binding). forward_static_call() calls a callable while keeping the current called class, where call_user_func() would reset it. These facts were measured on PHP 8.3.29 for the interactive tutorial at justinahampton.com/fun/experiments/forward-static-call/, where every output shown is captured from real PHP rather than typed in.
The rules (PHP 8.3)
forward_static_call($cb, ...$args)forwards the called class only when the called class is the callback's class or a descendant of it. Otherwise it behaves exactly likecall_user_func.- Called outside any class, or inside a plain function called from a method, it throws an uncaught
Error: "no class scope is active". - A bad callback throws
TypeError(the manual still says it returns false on error). - Arguments are passed by value; a by-reference parameter gets a Warning. Named arguments are rejected:
forward_static_call(..., by: 5)throwsArgumentCountError("does not accept unknown named parameters"). ['parent', 'm']as a callback forwards but is deprecated (since 8.2, per the manual).- In a non-static method,
call_user_func(['A', 'who'])also kept the object's class. - Closures pass through
forward_static_callfine.
Compared with the written forms
parent::who()keeps the called class;A::who()(a written class name) resets it to A;$class::$method()resets like a written name.parent::$method()with a string$methodforwards. With an array$methodit throws "Method name must be a string".parent::inside a trait used by a class with no parent throws "Cannot access "parent" when current class scope has no parent".
When forward_static_call is actually the right tool
- Skipping a generation: Admin extends User extends Model, and Admin wants Model's
all()while User overrides it.parent::all()runs User's override;Model::all()resetsstaticto Model;forward_static_call([Model::class, 'all'])runs Model's method withstaticstill Admin. - The callable is a value (a string or
[Class::class, 'method']chosen at runtime):parent::$step()cannot express an arbitrary target class,$step()resets the called class,forward_static_call($step)keeps it. - Where
parent::wins: by-reference arguments and named arguments both work withparent::method()and not with forward_static_call. The choice is about the destination: same chain, one level up, useparent::; anywhere else in the chain while keepingstatic, use forward_static_call.
Style note
Show modern syntax even when quoting old docs (Justin's preference): [A::class, 'test'] rather than array('A', 'test'), typed parameters, string ...$args, : void, no closing ?>.
Capturing real output for docs
The desktop has no PHP, so a capture script tars every snippet to the server, runs each with the server's PHP CLI in a temporary directory it deletes, and regenerates a truth.js the page reads. Rerun after editing any snippet; a test compares a small JS model of the rules with the captured outputs.