Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I properly check whether PHP is properly configured to use DOMDocument?

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading