get_class()

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 and the example the manual provides.

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.

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

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.

See also

get_called_class(), get_parent_class(), gettype(), get_debug_type(), is_subclass_of()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page get_class() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.