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

Take your skills to the next level ⚡️

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

No spam. Unsubscribe anytime.