What is the get_class method in PHP?

The get_class method can be used to get the class name of the object.

Syntax

get_class(object $object = ?): string

This method takes the object as an argument. The argument is optional if this method is called from inside the class method.

This method returns the class name.

Code

Let’s call the get_class method using the class object.

<?php
class MyClass {
function printClassName(){
echo get_class(). "\n";
}
}
$obj = new MyClass();
echo get_class($obj). "\n";
?>

In the code above, we have used the get_class method to get the class name of the object.

Code 2

Let’s call the get_class method inside the method of the class.

<?php
class MyClass {
function printClassName(){
echo get_class(). "\n";
}
}
$obj = new MyClass();
echo $obj->printClassName(). "\n";
?>

In the code above, we have called the get_class method inside the method of the MyClass.

Find more example here.

Free Resources