views:

47

answers:

2

It would take too long to explain why I need this, but I was wondering if there was a PHP function to check if a type of object was recognized. In other words, a function that would check if

$dog = new Dog();

would cause an error, because Dog didn't exist. Thanks for you help.

+4  A: 

There is a function called class_exists

For further explanation, see the PHP manual page.

http://php.net/manual/en/function.class-exists.php

nash
Awesome, thanks!
Ethan
+1  A: 

See class_exists. I think it is what you want. OR you can just wrap it with a try catch block.

Hope this helps.

NawaMan
A `try-catch`-block will not work as PHP will raise a fatal error when trying to use the variable holding the newly instantiated class. `class_exists` is by far the best option.
Stefan Gehrig