fun.sys / experiments / forward_static_call

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.

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.

the method echoes and the script calls

        

          
code ▸ marks the class the running method is written in (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.

call with

        

        

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.

the script calls
and B::run() contains

        
code ▸ the class the running method is written in. ◂ static the called class.
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.

kept the called class survived reset replaced by the class in the call none a plain function has no class

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.

the wrapper calls through

        

          

Do you need it? Often not, and it is worth saying so:

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.

the line is

        
Where the call from Admin lands. code ▸ the class whose method runs. ◂ static the called class.

          
you want to callparent::$method(...$args)forward_static_call()
the parent's version of a methodyes, prefer ityes
an ancestor further up, past an overridenoyes
a callable that arrives as a valuenoyes
a plain function or a closurenoyes, nothing to forward
with by-reference or named argumentsyesno

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.

Source: php.net manual, forward_static_call and its page on late static bindings. Outputs captured from PHP ; older versions may word errors differently. · ← experiments