P粉9738995672023-07-31 16:50:19
When a class is not found, an error is raised. This class does not inherit Exception, so your code cannot catch it.
The following code can solve this problem:
try { new DOMDocument(); } catch(Error $e) { echo 'DOMDocument not available'; }
or:
try { new DOMDocument(); } catch(Throwable $t) { echo 'DOMDocument not available'; }
Of course, you can directly use extension_loaded('dom') to detect whether the extension is available.
P粉5462579132023-07-31 16:03:24
You can use the class_exists() function to test whether a class exists. For example:
if (!class_exists('DOMDocument')){ echo "Please install DOMDocument"; exit; }