Check if a class exists in PHP

Posted on Sep 26, 2022

Learn how to check if a class exists in PHP

Photo from Unsplash

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! 👍

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.