forward_static_call()
A static method in PHP can ask two different questions: which class am I written in? and
which class was I called on? Most ways of calling another method keep the second answer. A callback usually loses it.
forward_static_call() is the callback call that keeps it.
Built from the PHP manual page for forward_static_call (PHP 5.3 and later). Every output on this page was printed by a real PHP , not typed in by hand.
- 1 · two questions
- 2 · the manual's example
- 3 · playground
- 4 · cheat sheet
- 5 · a use for it
- 6 · fine print
- 7 · quiz
- 8 · or parent::$method?
1Two questions
self:: means the class the line of code is written in. It never changes. static:: means the
called class: the class the call was made on, which can be a child that inherited the method. PHP calls this late static binding.
Change the keyword and the call below. The method lives in A either way; only static:: notices who was called.
self::).
◂ static marks the called class (static::).2The manual's example, one step at a time
The called class is set when a call starts. So what happens to it when one static method calls another? Some calls
forward it and some start over. The manual describes forward_static_call() as making a forwarding call,
"so static:: inside the method resolves to the same called class as in the caller."
This is Example #1 from the manual. The manual still writes it the PHP 5 way (array(), func_get_args(),
join()); here it is in current syntax, and it prints exactly the same three lines. Step through it, then switch the function to call_user_func()
and watch the second line of output change.
3Playground
Three classes in a line (C extends B extends A), an unrelated X, and a plain function, all with a
who() that reports what static:: is. B::run() contains one line. Choose that line and who the script calls.
show the whole program
4Cheat sheet
Every line from the playground, and what static:: is inside who() afterwards. Tap a result to load it above.
The rule underneath: parent::, self:: and static:: forward. A written-out class name and
call_user_func() start over. forward_static_call() forwards only when the called class is the callback's class or one of its
descendants, which is to say only when the call goes up the family tree. Anywhere else it behaves exactly like call_user_func().
5A use for it
A base class builds table names from static::class. A child adds one wrapper for every query method, so the method name
arrives as a string. If the wrapper drops the called class, Admin quietly queries the wrong table.
Do you need it? Often not, and it is worth saying so:
- If you know the method when you write the code,
parent::find($id)orstatic::find($id)already forward. - If only the method name is a variable,
parent::$method(...$args)forwards too. Part 8 shows where that stops being enough. - If the whole callable arrives as a value, such as an array or string built somewhere else (a hook list, a registry, configuration), and it points at an ancestor class, this function is the one that keeps the called class.
6Fine print
Small cases the manual mentions in a sentence, or not at all. Each one shows what PHP actually printed.
7Quiz
Same classes as the playground: C extends B extends A, unrelated X, and run() is written in B.
What is static:: inside who()?
8Why not parent::$method()?
Part 5 said parent::$method(...$args) forwards the called class too, and it does. What it cannot do is choose where the call goes.
It has one fixed destination: the method with that name, looked up from the class directly above the one the line is written in.
forward_static_call() takes the destination as a value, so it can be any ancestor, or any callable at all.
Three jobs. Try each line and see what PHP printed.
| you want to call | parent::$method(...$args) | forward_static_call() |
|---|---|---|
| the parent's version of a method | yes, prefer it | yes |
| an ancestor further up, past an override | no | yes |
| a callable that arrives as a value | no | yes |
| a plain function or a closure | no | yes, nothing to forward |
| with by-reference or named arguments | yes | no |
So the choice is about the destination. If the method you want is the one your parent would give you, write
parent::$method(...$args): it is plain call syntax, and references and named arguments work. Reach for
forward_static_call() when the destination is not "my parent": an ancestor further up the tree, or a callable somebody hands you.