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
get_class([object]);
Example
Get the class name for a created object:
<?php
class MyClass {}
$obj = new MyClass();
echo get_class($obj);
?>
Execution result:
'MyClass'
Example
Using the function without parameters inside a class method:
<?php
class TestClass {
public function showClassName() {
return get_class();
}
}
$test = new TestClass();
echo $test->showClassName();
?>
Execution result:
'TestClass'
Example
Attempting to get the class for a non-object:
<?php
$res = get_class('not an object');
var_dump($res);
?>
Execution result:
false
See Also
-
the get_parent_class function,
which returns the parent class -
the get_called_class function,
which returns the called class -
the is_a function,
which checks class membership