Using set_error_handler, you can do the following to convert any notifications / warnings caused by SimpleXMLElement into a catching Exception.
Do the following: -
<?php function getData() { return new SimpleXMLElement('http://10.0.1.1', null, true); } $xml = getData();
See how we get 2 warnings before throwing an exception from SimpleXMLElement? Well, we can convert them to Exception as follows: -
<?php function getData() { set_error_handler(function($errno, $errstr, $errfile, $errline) { throw new Exception($errstr, $errno); }); try { $xml = new SimpleXMLElement('http://10.0.1.1', null, true); }catch(Exception $e) { restore_error_handler(); throw $e; } return $xml; } $xml = getData();
Good luck
Anthony.
source share