HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

get_class

Description

The get_class of Class / Object for PHP 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.

Return

Returns the name of the class of which object is an instance.

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

1 · void · internal call

<?

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

        echo $return;
    }
}

$object = new myclass();

$object->myfunction();
myclass

2 · object · external call

<?

class myclass
{
}

$object = new myclass();

$return = get_class($object);

echo $return;
myclass

3 · superclass

<?

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

new myclass2();
myclass2
myclass1

4 · namespace

<?

namespace mynamespace;

class myclass
{
}

$object = new \mynamespace\myclass;

$return = get_class($object);

echo $return;
mynamespace\myclass