get_class(): Returns the name of the class of an object

get_class() — Returns the name of the class of an object.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog, the manual's example and further examples.

Signature

get_class(object $object = ?): string

Parameters

ParameterTypeWhat it takes
$objectobjectThe tested object.

Return value

Returns the name of the class of which object is an instance. If the object is an instance of a class which exists in a namespace, the qualified namespaced name of that class is returned.

Anything that is not an object raises a TypeError. To name the type of any value use gettype() (integer for 1) or get_debug_type() (int for 1, the class name for an object).

Example

A runnable snippet:

class foo { function name() { echo "My name is " , get_class($this) , "\n"; } } // create an object $bar = new foo(); // external call echo "Its name is " , get_class($bar) , "\n"; // internal call $bar->name();

Output:

Its name is foo My name is foo

A closure is an object too, of the class Closure. Here PHP 8.6 partial application creates one, and get_class() names it. This example needs PHP 8.6:

function add(int $a, int $b, int $c = 0): int { return $a + $b + $c; } $addOne = add(1, ?); var_dump(get_class($addOne)); var_dump($addOne(5));

string(7) "Closure" int(6)

Objects of built-in classes answer with the class name that PHP declares, printed here with var_dump():

$data = new stdClass(); $date = new DateTime(); $list = new ArrayObject([1, 2, 3]); var_dump(get_class($data)); var_dump(get_class($date)); var_dump(get_class($list));

string(8) "stdClass" string(8) "DateTime" string(11) "ArrayObject"

With inheritance the answer is the class that was instantiated, even when the variable is used as its parent type. get_parent_class() answers one level up, is_subclass_of() tests the relation, and get_called_class() names the class a static method was called on. The example:

class Animal { } class Dog extends Animal { } function describe(Animal $animal): string { return get_class($animal); } var_dump(describe(new Animal())); var_dump(describe(new Dog()));

string(6) "Animal" string(3) "Dog"

An enum case and a generator are objects too:

enum Suit { case Hearts; case Spades; } function numbers(): Generator { yield 1; } var_dump(get_class(Suit::Hearts)); var_dump(get_class(numbers()));

string(4) "Suit" string(9) "Generator"

In a catch block it tells which error really arrived, here after intdiv() divides by zero and DateTime gets a bad string:

try { intdiv(1, 0); } catch (Error $e) { var_dump(get_class($e)); } try { new DateTime("not a date"); } catch (Exception $e) { var_dump(get_class($e)); }

string(19) "DivisionByZeroError" string(28) "DateMalformedStringException"

A class declared in a namespace is returned with the namespace:

namespace Shop; class Order { } var_dump(get_class(new Order())); var_dump(get_class(new \stdClass()));

string(10) "Shop\Order" string(8) "stdClass"

The name of an anonymous class goes on after a NUL byte with the file and the line. strtok() keeps the readable part:

$logger = new class { }; $counter = new class implements Countable { public function count(): int { return 0; } }; var_dump(strtok(get_class($logger), "\0")); var_dump(strtok(get_class($counter), "\0"));

string(15) "class@anonymous" string(19) "Countable@anonymous"

Changelog

VersionChange
8.3.0Calling get_class() without an argument now emits an E_DEPRECATED warning; previously, calling this function inside a class returned the name of that class.
8.0.0Calling this function from outside a class, without any arguments, will now throw an Error. Previously, an E_WARNING was raised and the function returned false.
7.2.0Prior to this version the default value for object was null and it had the same effect as not passing any value. Now null has been removed as the default value for object, and is no longer a valid input.

The signature, parameters, return value, changelog and the first example come from the PHP manual page get_class() (PHP documentation, CC BY 3.0). Every example and its output was run in the official Docker images: the closure example on PHP 8.6.0 (php:8.6-cli), all the others on PHP 8.5.11 (php:8.5-cli).

Article authors: Andrei Olegovich, Klavdiya Kondakova