get_class

(PHP 4, PHP 5, PHP 7, PHP 8)

get_classReturns the name of the class of an object

Description

function get_class(object $object = ?): string

Gets the name of the class of the given object.

Parameters

object

The tested object.

Note:

Explicitly passing null as the object is no longer allowed as of PHP 7.2.0 and emits an E_WARNING. As of PHP 8.0.0, a TypeError is emitted when null is used.

Return Values

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.

Errors/Exceptions

If get_class() is called with anything other than an object, TypeError is raised. Prior to PHP 8.0.0, an E_WARNING level error was raised.

If get_class() is called with no arguments from outside a class, an Error is thrown. Prior to PHP 8.0.0, an E_WARNING level error was raised.

Changelog

Version Description
8.3.0 Calling 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.0 Calling 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.0 Prior 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.

Examples

Example #1 Using get_class()

<?php

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();

?>

The above example will output:

Its name is foo
My name is foo

Example #2 Using get_class() in superclass

<?php

abstract class bar {
    public function __construct()
    {
        var_dump(get_class($this));
        var_dump(get_class());
    }
}

class foo extends bar {
}

new foo;

?>

The above example will output:

string(3) "foo"
string(3) "bar"

Example #3 Using get_class() with namespaced classes

<?php

namespace Foo\Bar;

class Baz {
    public function __construct()
    {

    }
}

$baz = new \Foo\Bar\Baz;

var_dump(get_class($baz));
?>

The above example will output:

string(11) "Foo\Bar\Baz"

See Also

add a note

User Contributed Notes 37 notes

up
68
jjanak at webperfection dot net
11 years ago
>= 5.5

::class
fully qualified class name, instead of get_class

<?php
namespace my\library\mvc;

class Dispatcher {}

print Dispatcher::class; // FQN == my\library\mvc\Dispatcher

$disp = new Dispatcher;

print $disp::class; // parse error
up
32
dave at shax dot com
12 years ago
A lot of people in other comments wanting to get the classname without the namespace. Some weird suggestions of code to do that - not what I would've written! So wanted to add my own way.

<?php
function get_class_name($classname)
{
    if ($pos = strrpos($classname, '\\')) return substr($classname, $pos + 1);
    return $pos;
}
?>

Also did some quick benchmarking, and strrpos() was the fastest too. Micro-optimisations = macro optimisations!

39.0954 ms - preg_match()
28.6305 ms - explode() + end()
20.3314 ms - strrpos()

(For reference, here's the debug code used. c() is a benchmarking function that runs each closure run 10,000 times.)

<?php
c(
    function($class = 'a\b\C') {
        if (preg_match('/\\\\([\w]+)$/', $class, $matches)) return $matches[1];
        return $class;
    },
    function($class = 'a\b\C') {
        $bits = explode('\\', $class);
        return end($bits);
    },
    function($class = 'a\b\C') {
        if ($pos = strrpos($class, '\\')) return substr($class, $pos + 1);
        return $pos;
    }
);
?>
up
18
mail dot temc at gmail dot com
14 years ago
People seem to mix up what __METHOD__, get_class($obj) and get_class() do, related to class inheritance.

Here's a good example that should fix that for ever:

<?php

class Foo {
 function doMethod(){
  echo __METHOD__ . "\n";
 }
 function doGetClassThis(){
  echo get_class($this).'::doThat' . "\n";
 }
 function doGetClass(){
  echo get_class().'::doThat' . "\n";
 }
}

class Bar extends Foo {

}

class Quux extends Bar {
 function doMethod(){
  echo __METHOD__ . "\n";
 }
 function doGetClassThis(){
  echo get_class($this).'::doThat' . "\n";
 }
 function doGetClass(){
  echo get_class().'::doThat' . "\n";
 }
}

$foo = new Foo();
$bar = new Bar();
$quux = new Quux();

echo "\n--doMethod--\n";

$foo->doMethod();
$bar->doMethod();
$quux