I have a script that uses DOMDocument. In some environments, it fails, presumably due to some module is not loaded. What I’m trying to do is to provide a user of this script instructions to fix this behavior.
Here’s a minimal script to reproduce the problem:
<?php
echo 'start!';
try {
$doc = new DOMDocument();
} catch (Exception $e) {
echo 'catched';
}
echo 'end';
?>
If I open it in browser (served by my current server, involving Nginx), I see just start! (and the return code is 500; same output if I omit try..catch). So the problem is not only in detecting whether the right module is installed (should I use extension_loaded('dom') to check it?), but also the fact that try..catch doesn’t seem to work (I don’t get catched in the output; I’m using PHP 7.4.3 in the current case).
Any suggestions of how should I process this case correctly?
>Solution :
You can test for the class with class_exists()
Eg
if (!class_exists('DOMDocument')){
echo "Please install DOMDocument";
exit;
}