forward_static_call
forward_static_call(callable $callback, mixed ...$args): mixed
Forwards a static call down an inheritance chain while keeping late static binding (LSB): forward_static_call passes the "called class" along so static:: still resolves to the leaf subclass; a plain callback (call_user_func) falls back to the parent context.
C C23
#include <stdio.h>
struct base {
const char *name;
void (*who)(void);
};
static void a_who(void) { printf("A\n"); }
int main(void) {
/* C has no classes: a function pointer bound at assignment cannot know
who pointed at it - the call target is fixed early */
struct base b = {"B", a_who};
printf("b.name = %s; b.who prints: ", b.name);
b.who();
return 0;
}
b.name = B; b.who prints: A
C++ C++26
#include <iostream>
struct Base {
static void who() { std::cout << "Base\n"; }
static void test() { who(); } // plain static call, no dynamic dispatch
};
struct Derived : Base {};
int main() {
// static members are not polymorphic: the name is resolved at compile time
Derived::test(); // Base (the CRTP idiom exists precisely to fake this)
return 0;
}
Base
C# v11.0.100
B.Test(); // A: static methods are not polymorphic in C# either
class A
{
public static void Who() => Console.WriteLine("A");
public static void Test() => Who(); // static dispatch, no late binding
}
class B : A {}
A
Go v1.27.4
package main
import "fmt"
func Who() { fmt.Println("top-level Who") }
func Test() { Who() } // plain function call: no "called class" to forward
func main() {
Test() // always prints the same thing: no inheritance, no LSB
}
top-level Who
Java v27.0.1
public class Main {
static class A {
static void who() { System.out.println("A"); }
static void test() { who(); } // static call: no receiver, no late binding
}
static class B extends A {}
public static void main(String[] args) {
B.test(); // A: static methods are not polymorphic in Java
}
}
A
JavaScript v26.13.1
class A {
static who() { console.log(this.name); }
}
class B extends A {
static test() { super.who(); } // super is A; this is still B: late binding
}
B.test(); // B - the receiver survives the super hop
A.who(); // A
B A
PHP v8.6.0
class A {
public static function who() { echo static::class, "\n"; }
// forward_static_call forwards the CALLED class down the hop:
// static:: inside who() still resolves to B
public static function viaForward() { forward_static_call([__CLASS__, 'who']); }
// a plain callback rebuilds the call context from A: LSB is lost
public static function viaCallUserFunc() { call_user_func([__CLASS__, 'who']); }
}
class B extends A {}
B::viaForward(); // B - called class forwarded
B::viaCallUserFunc(); // A - forwarding context lost
B A
Python v3.15.2
class A:
@classmethod
def who(cls):
print(cls.__name__)
class B(A):
@classmethod
def test(cls):
super().who() # zero-arg super forwards cls: the subclass survives
B.test() # B - cls arrives at who() still bound to B
A.who() # A
B A
Rust v1.99.1
// no classes in Rust: a trait default method plus an associated const
// is the closest thing to late static binding - each impl names itself
trait Who {
const NAME: &'static str;
fn who(&self) {
println!("{}", Self::NAME);
}
}
struct B;
impl Who for B {
const NAME: &'static str = "B";
}
fn main() {
B.who(); // resolved for the concrete impl, not the trait
}
B
TypeScript v26.13.1
class A {
static who(): void { console.log(this.name); }
}
class B extends A {
static test(): void { super.who(); } // super is A; this is still B: late binding
}
B.test(); // B - the receiver survives the super hop
A.who(); // A
B A
Language differences
c: C has no classes or inheritance: a function pointer is fixed the moment it is assigned - storing a name in the struct cannot change who it points to. Printing A is the most honest demo of "early binding".
cpp: static member functions are not polymorphic (there is no virtual static): who inside Derived::test() resolves to Base::who at compile time. To replicate LSB in C++, use the CRTP (Curiously Recurring Template Pattern): the template base "knows" the leaf subclass through the Derived type parameter.
csharp: same as Java: static methods don't enter the vtable, and B.Test() still reaches the A.Who inside A.Test - static dispatch, no late binding.
go: no classes, no inheritance: package-level functions are the most "static" thing there is, call targets pinned at compile time; generic parameters can pass the "caller type" into a function at compile time - the embryo of a static LSB.
java: static methods are not polymorphic: B.test() first dispatches statically by declared type to A.test, and who() in turn resolves statically inside A - two early bindings stacked, the output is constantly A. This is a JVM design decision: static belongs to the class, not to instances.
javascript: a half-match: inside a static method, super points at the parent class (hence who lives in parent A and test in child B - this example's shape), while this stays the caller B - "forwarding the called class" comes for free from this's dynamic binding, isomorphic to PHP's static::.
python: a half-match: the classmethod's cls argument plus zero-arg super() performs the forwarding (super(B, cls) hands cls to A.who untouched) - it likewise requires who in the parent and test in the child, same shape as JS; metaclass tricks go further but are beyond comparative teaching.
rust: no classes, no inheritance: a trait default method plus an associated const (the example) is the stand-in for "compile-time late binding" - each impl names itself with its own NAME, and the call resolves to the concrete type rather than the trait; generic monomorphization guarantees zero runtime cost.
typescript: same as JavaScript at runtime (identical super/this semantics); at the type level the static side's ability to declare this's type is weaker than the instance side - a harmless difference.
php: the LSB (Late Static Binding) core: static:: resolves by the "called class". forward_static_call forwards the called class down the chain - inside B::viaForward(), who()'s static::class is still B; call_user_func rebuilds the context where the called class falls back to A (prints A). Static methods only (no $this forwarding), commonly used for "redispatch" inside trait/parent methods.
common ground: PHP's LSB is a dynamic-language specialty - static:: resolves by the caller at runtime; Java/C# static methods are pinned at compile time; JS/Python get half the ability via this/cls passing; C/Go/Rust solve it at compile time with function pointers, package functions, and generic monomorphization.