get_class

Returns the name of the class of an object

Syntax

get_class ( [ object $object ] ) : string

Parameters

object

The tested object. This parameter may be omitted when inside a class.

Note: Explicitly passing NULL as the object is no longer allowed as of PHP 7.2.0. The parameter is still optional and calling get_class() without a parameter from inside a class will work, but passing NULL now emits an E_WARNING notice.

Return

Returns the name of the class of which object is an instance. Returns FALSE if object is not an object.

If object is omitted when inside a class, the name of that class is returned.

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

Examples

void

<?

class myclass
{
    function __construct()
    {
        $return = get_class();

        echo $return;
    }
}

new myclass();

?>
myclass

object | Internal Call

<?

class myclass
{
    function myfunction()
    {
        $return = get_class($this);

        echo $return;
    }
}

$object = new myclass();

$object->myfunction();

?>
myclass

object | External Call

<?

class myclass
{
}

$object = new myclass();

$return = get_class($object);

echo $return;

?>
myclass

1

<?

class myclass1
{
    function __construct()
    {
        echo get_class($this) . PHP_EOL;
        echo get_class();
    }
}
class myclass2 extends myclass1
{
}

new myclass2();

?>
myclass2
myclass1

2

<?

namespace mynamespace;

class myclass
{
}

$object = new \mynamespace\myclass;

$return = get_class($object);

echo $return;

?>
mynamespace\myclass

PHP

Home Menu