简体   繁体   English

检查一个php变量是否包含一个SimpleXMLObject

[英]Check a php variable contains a SimpleXMLObject

I need to check that the $model variable contains an object of type SimpleXMLObject. 我需要检查$ model变量是否包含SimpleXMLObject类型的对象。

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

This is the end of the convertToSimpleXml method where the object is returned using simplexml_load_file 这是convertToSimpleXml方法的结尾,在其中使用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 = simplexml_load_file($path);
if($simpleXml instanceof SimpleXMLElement) {
  return $simpleXml;
} else {
  return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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