Thursday, October 23, 2025
HomeLanguagesPHP | class_exists() Function

PHP | class_exists() Function

The class_exists() function is an inbuilt function in PHP which is used to check whether the given class is defined or not.

Syntax:

bool class_exists( string $class_name, bool $autoload = TRUE )

Parameters: This function accept two parameters as mentioned above and described below:

  • $class_name: It holds the class name which need to check their existence.
  • $autoload: It checks whether the __autoload is called or not by default.

Return Value: This function returns True if class name is defined otherwise returns False.

Below programs illustrate the class_exists() function in PHP:

Program 1:




<?php
  
// Create a class
class GFG {
    public $Geek_name = "Welcome to neveropen"; 
} 
  
// Check class name exist or not
if(class_exists('GFG')) {
    echo "Class name exists";
}
else {
    echo "Class name does not exist";
}
  
?>


Output:

Class name exists

Program 2:




<?php
  
// Creating class 
class GFG { 
    public $data1; 
    public $data2; 
    public $data3; 
}
  
if(class_exists('GFG')) {
  
    // Creating an object 
    $obj = new GFG();
  
    // Set values of $obj object 
    $obj->data1 = "Geeks"; 
    $obj->data2 = "for"; 
    $obj->data3 = "Geeks"; 
  
    // Print values of $obj object 
    echo "$obj->data1  \n$obj->data2  \n$obj->data3"; 
}
else {
    echo "Class does not exist";
}
  
?>


Output:

Geeks  
for  
Geeks

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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS