get_called_class
Returns the name of the class a static method was actually called on (late static binding).
get_called_class(): string- Added in PHP 5.3.0
Return value
stringReturns the name of the class the static method was called on.
Errors
Throws an Error when called from outside a class context.
Changelog
- PHP 8.0.0Calling this function from outside a class now throws an Error. Previously it raised an E_WARNING and returned false.
get_called_class() exposes late static binding as a plain function. Inside a static method it returns the class name that appeared on the left of the :: at the call site, not the class where the method body was written. Called inside an instance method it returns the object's class, exactly like get_class($this).
When to reach for it
Its classic home is an inherited static factory or an active-record base class: the parent defines create() once, and each child gets an instance of itself because the parent asks who it was called on. It is also handy in trait methods that need to log or key a cache by the consuming class.
Things that trip people up
The confusion is almost always with self. self::class is resolved at compile time and always yields the class that wrote the code; get_called_class() and static::class are resolved at call time and yield the class that was invoked. In modern PHP, static::class does the same job with no function call, is visible to static analysers, and reads better — treat get_called_class() as the legacy spelling and prefer static::class in new code.
Note also that the answer depends on the call site: $parentInstance::method() and forwarding calls such as parent::method() preserve the originally called class.
Example
When to use it
- Let a base model's static create() return an instance of whichever subclass was called
- Key a static cache or registry by the concrete class using a shared trait
- Build error messages in an abstract base that name the concrete subclass
Frequently asked questions
What is the difference between get_called_class and get_class?
get_class() needs an object and reports that object's class, while get_called_class() takes no arguments and reports the class a static method was invoked on. In an instance method they give the same answer.
Should I use get_called_class or static::class?
Prefer static::class in new code — it is identical in meaning, avoids a function call, and static analysis tools understand it. get_called_class() remains for compatibility with pre-5.5 codebases.
Why does get_called_class throw an Error?
Because you called it outside any class context, such as at the top level of a script or inside a plain function. Since PHP 8.0 that is an Error rather than a warning returning false.
More examples
An inherited static factory
One make() body produces a Post for Post::make() and a Tag for Tag::make(). Had the body used new self(), both calls would have tried to instantiate the abstract Record and thrown an Error.
<?php
abstract class Record {
public array $data = [];
public static function make(array $data): static {
$class = get_called_class();
$obj = new $class();
$obj->data = $data;
return $obj;
}
}
class Post extends Record {}
class Tag extends Record {}
var_dump(get_class(Post::make(['t' => 'hi'])));
var_dump(get_class(Tag::make(['n' => 'php'])));Instance context and the outside-a-class error
In an instance method the function reports the object's real class. Called at the top level it throws an Error in PHP 8.0 and later; older versions only warned and returned false, so old code that tested for false no longer works.
<?php
class Base {
public function who(): string { return get_called_class(); }
}
class Child extends Base {}
echo (new Child())->who(), "\n"; // Child - same as get_class($this)
try {
echo get_called_class(), "\n";
} catch (Error $e) {
echo 'Error: ', $e->getMessage(), "\n";
}
Discussion