I can't do this but wondering what would work:
is_object(new Memcache){
//assign memcache object
$memcache = new Memcache;
$memcache->connect('localhost', 11211);
$memcache->get('myVar');
}
else{
//do database query to generate myVar variable
}
You can use class_exists()
to check if a class exists, but it will not return if you can instantiate that class!
One of the reasons you can't, might be that it is an abstract class. To check for that you should do something like this after you check for class_exists()
.
This might be impossible (having an abstract class, not checking for it) for above example, but in other situations might be giving you headaches :)
//first check if exists,
if (class_exists('Memcache')){
//there is a class. but can we instantiate it?
$class = new ReflectionClass('Memcache')
if( ! $class->isAbstract()){
//dingdingding, we have a winner!
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With