Check if a class exists in PHP

To check whether a class exists or not in PHP, you need to use the class_exists() function.

Call the class_exists() function and pass the class name as a string parameter to that function:

// ๐Ÿ‘‡ check if a class named Human exists
class_exists("Human");

The function will return either true or false. Combine it with an if block to create conditional branch as shown below:


if(class_exists("Human")){
    print "The Human class exists";
} else {
    print "Class does not exists";
}

And that’s how you check whether a class exists or not in PHP. Nice! ๐Ÿ‘

More PHP tutorials

See all