get_called_class

get_called_class(): string

Returns the class a static method was actually called on (late static binding); the functional form of static::class.

C C23

C23
#include <stdio.h>

/* C has no classes or static dispatch context - the closest emulation is
   a vtable of function pointers bound at construction time */
typedef struct {
    const char *(*who)(void);
} Base;

static const char *base_who(void) { return "Base"; }
static const char *derived_who(void) { return "Derived"; }

int main(void) {
    Base b = { base_who };
    Base d = { derived_who };
    printf("%s\n", b.who()); /* each table knows only its own name */
    printf("%s\n", d.who());
    return 0;
}
Output
Base
Derived

C++ C++26

C++26
#include <iostream>

class Base {
public:
    virtual ~Base() = default;
    virtual const char* who() const { return "Base"; }
};
class Derived : public Base {
public:
    const char* who() const override { return "Derived"; }
};

int main() {
    Base* p = new Derived();
    // virtual dispatch picks the most-derived override, but every name is
    // resolved at COMPILE time - no "called class" exists at runtime
    std::cout << p->who() << "\n";
    std::cout << p->Base::who() << "\n"; // qualified call bypasses dispatch
    delete p;
    return 0;
}
Output
Derived
Base

C# v11.0.100

v11.0.100
class Base {
    // static methods bind at COMPILE time - C# has no late static binding
    public static string Who() { return "Base"; }
}
class Derived : Base {}

class MainClass {
    static void Main() {
        System.Console.WriteLine(Base.Who());
        System.Console.WriteLine(Derived.Who()); // still "Base": the
        // definition site decides, never the call site
        // the instance-side runtime type is a different, real concept:
        System.Console.WriteLine(new Derived().GetType().Name);
    }
}
Output
Base
Base
Derived

Go v1.27.4

v1.27.4
package main

import "fmt"

type Base struct{ name string }

func (b Base) Who() string { return b.name }

type Derived struct{ Base }

func main() {
	// embedding is composition: the promoted method keeps the embedded
	// value, so there is no late binding anywhere
	d := Derived{Base{"Base"}}
	fmt.Println(d.Who())      // promoted method: the Base field's name
	fmt.Println(d.Base.Who()) // explicit qualification: identical
}
Output
Base
Base

Java v27.0.1

v27.0.1
class Base {
    // static methods bind at COMPILE time - Java has no late static binding
    static String who() { return "Base"; }
}
class Derived extends Base {}

class Main {
    public static void main(String[] args) {
        System.out.println(Base.who());
        System.out.println(Derived.who()); // still "Base": the definition
        // site decides, never the call site
        // the instance-side runtime type is a different, real concept:
        System.out.println(new Derived().getClass().getSimpleName());
    }
}
Output
Base
Base
Derived

JavaScript v26.13.1

v26.13.1
// JS statics DO have late binding: inside a static method, `this` is the
// class the method was CALLED on - a true get_called_class counterpart
class Base {
    static who() { return this.name; }
}
class Derived extends Base {}

console.log(Base.who());
console.log(Derived.who()); // this === Derived at the call site
Output
Base
Derived

PHP v8.6.0

v8.6.0
// LSB: the class a static method was CALLED on, not where it is defined
class Base {
    public static function who(): string {
        return get_called_class(); // same value as static::class (5.5+)
    }
}
class Derived extends Base {}

echo Base::who(), "\n";    // called on Base
echo Derived::who(), "\n"; // defined in Base, called on Derived
$d = new Derived();
echo $d->who(), "\n";      // instance access keeps the object's class
// called from outside any class: throws Error (since 8.0; was E_WARNING)
Output
Base
Derived
Derived

Python v3.15.2

v3.15.2
# classmethods receive the class they were CALLED on: the full
# get_called_class counterpart in a dynamic language
class Base:
    @classmethod
    def who(cls):
        return cls.__name__


class Derived(Base):
    pass


print(Base.who())
print(Derived.who())  # cls is Derived at the call site
Output
Base
Derived

Rust v1.99.1

v1.99.1
// no inheritance, no static context: trait default methods are
// monomorphized per implementor, so each type statically gets its own name
trait Named {
    fn name() -> &'static str;
    // a default method CANNOT name the implementor at runtime - every
    // implementor supplies the name itself
    fn who() -> &'static str { Self::name() }
}

struct Base;
struct Derived;

impl Named for Base {
    fn name() -> &'static str { "Base" }
}
impl Named for Derived {
    fn name() -> &'static str { "Derived" }
}

fn main() {
    println!("{}", Base::who());
    println!("{}", Derived::who()); // distinct monomorphized code
}
Output
Base
Derived

TypeScript v26.13.1

v26.13.1
// same runtime semantics as JS: `this` inside a static is the class it
// was called on - the late-bound counterpart
class Base {
    static who(): string { return this.name; }
}
class Derived extends Base {}

console.log(Base.who());
console.log(Derived.who()); // this === Derived at the call site
Output
Base
Derived

Language differences

C: no classes, no static dispatch context - "the called class" does not exist; function-pointer tables bind at initialization (example), the call site is a runtime non-concept.

C++: virtual dispatch picks the most-derived override, but every name resolves at COMPILE time; a qualified call p->Base::who() bypasses dispatch (example). typeid(*p).name() gives the dynamic type name but is implementation-defined and non-portable, so it stays out of the example.

C#: static methods bind at compile time, no late static binding - Derived.Who() still prints the definition site's "Base" (example); GetType().Name on the instance side is a different, real concept.

Go: embedding is composition, not inheritance; promoted methods act on the embedded value, no late binding (example prints the same value twice); no static context at all.

Java: fully isomorphic to C# - static methods bind at compile time (example); getClass() is the instance-side runtime type.

JavaScript: a TRUE counterpart - inside a static method, this is the class it was called on (example), and this.name is that class's name; one of the few spots where JS is MORE late-bound than Java/C#.

PHP: the LSB core - get_called_class() returns the call-site class; static::class (5.5+) is the same value in modern spelling; pairs with the existing forward_static_call entry (forwarding calls preserve LSB); since 8.0 calling it outside a class throws Error; note the method must be static and reached via the static chain - instance access yields the object's class (example).

Python: a FULL counterpart - the cls of a @classmethod is exactly the call-site class (example); staticmethod has no cls and cannot get it.

Rust: no inheritance, no static context; trait default methods monomorphize per implementor and Self:: is fixed at compile time (example); a "call-site name" is only possible if each implementor supplies its own.

TypeScript: runtime semantics identical to JS (example); the static-side this can be annotated with a this-parameter at the type level.