Checking for Multiple Classes with class_exists

This is a rare day indeed. It’s not often that I post something about php. I’m really just starting to dig into it in earnest. I’ve always basically just “hacked” my way through php code to get the results that I wanted. This is more or less the same thing I did with the example… Read more Checking for Multiple Classes with class_exists

This is a rare day indeed. It’s not often that I post something about php. I’m really just starting to dig into it in earnest. I’ve always basically just “hacked” my way through php code to get the results that I wanted. This is more or less the same thing I did with the example below, but the difference is that (1) I actually read the PHP manual to get me in the right direction; and (2) I seriously could not find this answer anywhere on the Interwebs. Which meant I absolutely HAD to write it up here.

But watch for “the problem” at the end too.

What is class_exists?

It’s a PHP function which checks whether a class exists. That’s what’s great about PHP, you can almost figure out what it’s doing by just reading it like a sentence. In this case, if the class does actually exist, then whatever code you put next gets executed.

Why Would Anyone Care about that?

Well, in my case, I was trying to check to see if either one of two different plugins were activated in an installation, and if either of them were, I would add an admin notice to give the user info about what to do. I’ve seen others who hide or show settings in their plugins based on whether a certain plugin was activated or not as well.

You see, WordPress is built on PHP, so it inherits all these functions natively. But since it’s a PHP function, you’re not going to find a Codex article on how to use class_exists in WordPress. Instead, you have to actually learn PHP itself (ug!).

So, here’s the PHP manual on that. You’ll notice it never says whether you can add multiple classes or not, it just shows you how to use the one. So, there’s a few other handy “logical operators” that I should be able to use to check “if” this class exists “or” that class exists “only then” execute the code.

So here’s all the failed experiments:

if (class_exists('class1' or 'class2'){}
//WRONG! COLD!
if (class_exists('class1' || 'class2'){}
//STILL WRONG! COLDER
if (class_exists('class1') or ('class2'){}
//DUMBA$$ NOW YOU'RE FROZEN AND NOT IN THE "LET IT GO" KINDA WAY

But no one cares about all my wrong answers really, you just want the RIGHT answer. Well, I decided that I knew I could use the logical operators for multiple variables, so how about I define them with class_exists. BAM!

$class1 = class_exists('class1');
$class2 = class_exists('class2');
if ($class1 || $class2) {}
//NOW YOU'RE TALKIN'!

So, what’s the problem?

The problem is that there’s always another way to skin a cat, or make a better mouse trap (or some other animal analogy). I’m certain others have found ways to check for multiple classes and their code is leaner and meaner. So, the problem is: You’re all smarter than me! I’d love to hear your solution in the comments.

5 thoughts on “Checking for Multiple Classes with class_exists

    1. Hi Kenth, that looks like a really useful function. Will try that out as well. I wonder if there are potential performance differences between these methods as well.

  1. I would do if ( class_exists('class1') || class_exists('class2') ) { } directly. Every core PHP function is just like your own PHP functions where it expects an exact type and number of arguments. Although sometimes it gets really confusing with certain functions, such as array manipulation where arguments are passed by reference instead of value and you get something random as a return value.

    1. I’m 99% certain I tried that as well. I didn’t want to list ALL of the things I tried in order to get it to work, no one wants to read THAT list! Thanks for dropping by!

Leave a Reply

Your email address will not be published. Required fields are marked *