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.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources