PHP:错误检查错误
if($xml->getElementsByTagName($elmnt) && $xml->getElementsByTagName($elmnt)->length > 0)
该行旨在检查错误。我想要的只是制作一条清晰的错误消息,而不是破坏整个页面。它包含在一个函数中,该函数旨在在失败时停止所有相关进程,并在不起作用时继续显示页面的其余部分,因为页面布局不取决于此函数是否成功。
致命错误:在 file.php 的第 100 行上的非对象上调用成员函数 getElementsByTagName()
我如何实际检查以确保 有问题的 DOMDocument
具有该元素,但没有抛出上面的错误?我尝试过仅使用第一个条件或第二个条件。
var_dump($xml);
object(DOMDocument)#3 (1) {
["preserveWhitespace"]=>
bool(false)
}
if($xml->getElementsByTagName($elmnt) && $xml->getElementsByTagName($elmnt)->length > 0)
This line is intended to check for errors. All I want is to, instead of breaking the entire page, make a legible error message. It is included in a function designed to stop all related processes on failure and continue displaying the rest of the page if it doesn't work, since the page layout does not depend on whether or not this function succeeds.
Fatal error: Call to a member function getElementsByTagName() on a non-object in file.php on line 100
How do I actually check to make sure that the DOMDocument
in question has the element without it throwing the error above? I've tried using just the first condition or the second condition.
var_dump($xml);
object(DOMDocument)#3 (1) {
["preserveWhitespace"]=>
bool(false)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果使用 simplexml_load_file 加载文件,它将返回加载成功或失败时返回 false。您收到的错误表明变量 $xml 不是对象,这意味着当您尝试将 xml 文件加载到该变量时,它没有成功。
如果在执行 getElementsByTagName($elmnt) 之前确保 $xml 不为 false,则不应收到错误 Fatal error: Call to a member function getElementsByTagName() on a non-object in file.php on第 100 行
If you use simplexml_load_file to load the file, it will either return with a successfull load or it will return false on failure. The error you are getting signifies that the variable $xml is not an object, which means that when you tried to load the xml file onto the variable, it didn't do it successfully.
If before you do the getElementsByTagName($elmnt) you make sure $xml is not false, you shouldn't get the error Fatal error: Call to a member function getElementsByTagName() on a non-object in file.php on line 100
你的问题是
$xml
不是一个对象,你必须首先检查:你也可以使用
!empty($xml)
而不仅仅是$xml
如果$xml
可能未定义。Your problem is that
$xml
is not an object, you have to check that first:You can also use
!empty($xml)
instead of just$xml
if there is a possibility that$xml
is undefined.