Friday, November 14, 2025
HomeLanguagesPHP | get_class() Function

PHP | get_class() Function

The get_class() function is an inbuilt function in PHP which is used to return the class name of an object.

Syntax:

string get_class( object $object )

Parameters: This function accepts single parameter $object which holds the object that need to be tested. The value of this parameter can be omitted inside the class.

Return Value: This function returns the class name of which object is an instance. It returns FALSE if the object is not an object. If the object is omitted when inside the class then the class name is returned.

Below programs illustrate the get_class() function in PHP:

Program 1:




<?php
  
// Create a class
class GFG {
  
    public function Geeks() {
        echo "Class name: " . get_class($this);
    }
}
  
// Create an object
$obj = new GFG();
  
$obj->Geeks();
  
?>


Output:

Class name: GFG

Program 2:




<?php
  
// Creating class 
class GFG { 
    public $data1; 
    public $data2; 
    public $data3; 
}
  
if(class_exists('GFG')) {
      
    $obj = new GFG();
      
    echo "Class name: " . get_class($obj);
}
else {
    echo "Class does not exist";
}
  
?>


Output:

Class name: GFG

Reference: https://www.php.net/manual/en/function.get-class.php

RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7142 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6840 POSTS0 COMMENTS