get_class
Returns the name of the class an object is an instance of.
get_class(object $object = ?): string- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$objectoptional | object= null | The object to inspect. Omitting it is deprecated as of PHP 8.3.0; pass the object explicitly. |
Return value
stringReturns the name of the class of which object is an instance.
Errors
Throws a TypeError if the argument is not an object, and an Error if called with no argument from outside a class.
Changelog
- PHP 8.3.0Calling get_class() without an argument emits an E_DEPRECATED diagnostic. Previously, calling it inside a class returned the name of that class.
- PHP 8.0.0Calling the function from outside a class without any arguments now throws an Error. Previously an E_WARNING was raised and the function returned false.
- PHP 7.2.0Passing null explicitly is no longer valid input; before 7.2 null was the default and behaved the same as omitting the argument.
get_class() returns the fully qualified class name of an object, including its namespace and with no leading backslash. It always reports the concrete class, never a parent or an interface the object happens to implement.
When to reach for it
Use it for diagnostics: log lines, exception messages, debug dumps, and cache keys built from a handler's type. It is also the usual way to map an object back to a configured class-string, for example looking up a serializer registered under Order::class.
In PHP 8.0 and later, $object::class does the same job with less ceremony and is the preferred spelling in new code. get_class() still wins when you already hold the value in an expression that you would otherwise have to assign to a variable.
Things that trip people up
- It is not a type check. Comparing
get_class($x) === Animal::classis false for every subclass; useinstanceofunless you deliberately want an exact-class match. - Calling it with no argument inside a class used to return the current class. That is deprecated since PHP 8.3 — write
static::classorself::classinstead. - Anonymous classes return a generated name containing
class@anonymousplus the defining file and a NUL byte, which is not safe to display or compare.
Example
When to use it
- Include the concrete handler type in a log line or exception message
- Look up configuration or a serializer keyed by an object's class-string
- Assert an exact class (not a subclass) in a test
Frequently asked questions
What is the difference between get_class and instanceof?
get_class() returns the exact class name as a string, while instanceof asks whether an object is of a type or any of its subtypes or interfaces. Use instanceof for type checks and get_class() only when you need the literal name.
Is get_class deprecated in PHP 8?
No, the function is fine. What was deprecated in PHP 8.3 is calling it with no argument inside a class; use self::class or static::class there, and $obj::class to get an object's class.
Does get_class include the namespace?
Yes. It returns the fully qualified name, such as App\Models\User, with no leading backslash — which matches what User::class produces.
More examples
Dispatching on an exact class
The class-string returned by get_class() is used directly as a map key, which is the pattern that makes this function worth keeping. Note that a subclass of Circle would miss the map entirely — that is the trade-off of exact-class dispatch.
<?php
class Circle { public float $r = 2.0; }
class Square { public float $s = 3.0; }
$area = [
Circle::class => fn(Circle $c) => M_PI * $c->r ** 2,
Square::class => fn(Square $s) => $s->s ** 2,
];
foreach ([new Circle(), new Square()] as $shape) {
printf("%s => %.2f\n", get_class($shape), $area[get_class($shape)]($shape));
}TypeError and anonymous classes
Anonymous classes get a generated name tied to the file and line, so it must never be persisted or compared. Passing a non-object throws a TypeError rather than returning false, so guard with is_object() when the input is untrusted.
<?php
$anon = new class { public int $n = 1; };
$name = get_class($anon);
echo str_contains($name, 'class@anonymous') ? "anonymous class\n" : "named\n";
echo strlen($name) > 15 ? "name is long and file-specific\n" : "";
try {
get_class('not an object');
} catch (TypeError $e) {
echo 'TypeError caught', "\n";
}
Discussion