Php 为什么我会出现这样的错误:“;传递给myFunction()的参数1必须是SimpleXMLElement的实例,如果给定“null”,则为空;?
我不断地发现这个错误: 传递给myFunction()的参数1必须是SimpleXMLElement的实例,如果为空 这是我的密码:Php 为什么我会出现这样的错误:“;传递给myFunction()的参数1必须是SimpleXMLElement的实例,如果给定“null”,则为空;?,php,simplexml,type-hinting,Php,Simplexml,Type Hinting,我不断地发现这个错误: 传递给myFunction()的参数1必须是SimpleXMLElement的实例,如果为空 这是我的密码: function myFunction(SimpleXMLElement $order_status_response_element) { // Get any invoices firstly. $invoice_details_element_array = $order_status_response_element->xpath('
function myFunction(SimpleXMLElement $order_status_response_element) {
// Get any invoices firstly.
$invoice_details_element_array = $order_status_response_element->xpath('//Detail/RefInfo/RefIDQual[text()="IN"]/parent::*/parent::*');
$invoice_numbers_element_array = $order_status_response_element->xpath('//Detail/RefInfo/RefIDQual[text()="IN"]/parent::RefInfo/RefID');
$non_invoice_details_element_array = $order_status_response_element->xpath('//Detail[not(RefInfo/RefIDQual[text()="IN"])]');
if(count($non_invoice_details_element_array) > 1) throw new Exception('More than one instance of non-invoiced details in array! Aborting!');
$non_invoice_details_element = $non_invoice_details_element_array[0];
echo("non_invoice_details_element is a SimpleXMLElement, true? ".is_a($non_invoice_details_element,'SimpleXMLElement')."\n");
$non_invoice_detail = sXMLtoArray($non_invoice_details_element);
^^^ERROR HAPPENS HERE^^^
}
/**
* Return an array, given a SimpleXMLElement object.
*
* @param SimpleXMLElement $elem
* @return array
*/
function sXMLtoArray(SimpleXMLElement $elem) {
return json_decode(json_encode($elem),TRUE);
}
现在,我得到的输出如下所示:
non_invoice_details_element is a SimpleXMLElement, true? 1
Argument 1 passed to sXMLtoArray() must be an instance of SimpleXMLElement, null given, called in {path} on line 2033 and defined {paths}
因此,如果is_a()
告诉我它确实是一个simplexmlement,那么当它被传递到sXMLtoArray()
函数时,为什么它认为它是空的呢?如果它不是SimpleXMLElement,那么它将抛出一个异常,因为$non_invoice_details_element_数组的长度为零,因为xpath不存在
这到底是怎么回事?var\u dump($non\u invoice\u details\u element)?看起来像一个PHP错误!:如果你的xpath查询真的找到了你要找的节点,我想这是因为我的函数不在一个对象内,也就是说,它不是一个“公共函数”,它只是一个普通的“函数”。。。不知何故,在这些问题上设置暗示是把事情搞砸了。我将做更多的测试,看看问题是否在于此或其他。当有多个结果时,您会抛出一个异常。您不会检查零结果。