olliejjc16
olliejjc16

Reputation: 371

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?

Upvotes: 0

Views: 38

Answers (1)

maxhb
maxhb

Reputation: 8845

You can simply check the class of your $simpleXml:

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

Upvotes: 2

Related Questions