The get_class Function

The get_class function returns the name of the class of the passed object. If the function is called without parameters inside a class method, it will return the name of that class. If a non-object is passed, the function will return false and generate a warning.

Syntax

phpget_class([object]);

Example

Get the class name for a created object:

php<?php
  class MyClass {}
  $obj = new MyClass();
  echo get_class($obj);
?>

Execution result:

php'MyClass'

Example

Using the function without parameters inside a class method:

php<?php
  class TestClass {
    public function showClassName() {
      return get_class();
    }
  }
  $test = new TestClass();
  echo $test->showClassName();
?>

Execution result:

php'TestClass'

Example

Attempting to get the class for a non-object:

php<?php
  $res = get_class('not an object');
  var_dump($res);
?>

Execution result:

phpfalse

See Also