简体   繁体   中英

Check a php variable contains a SimpleXMLObject

I need to check that the $model variable contains an object of type SimpleXMLObject.

$model = convertToSimpleXml($fileName, $filePath);

This is the end of the convertToSimpleXml method where the object is returned using simplexml_load_file

$simpleXml = simplexml_load_file($path);
return $simpleXml;

I've tried checking it as an array or something similar but no luck with that, have looked around for examples but can't find any clear cut answer to the question. Can anyone help?

You can simply check the class of your $simpleXml :

$simpleXml = simplexml_load_file($path);
if($simpleXml instanceof SimpleXMLElement) {
  return $simpleXml;
} else {
  return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM