我一直收到这个错误:
Argument 1 passed to myFunction() must be an instance of SimpleXMLElement, null given
这是我的代码:
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()
告诉我它确实是一个SimpleXMLElement,那么当它传递给sXMLtoArray()
函数时为什么认为它为空?如果它不是SimpleXMLElement,那么它之前会抛出异常,因为$ non_invoice_details_element_array将是零长度,因为xpath不存在。
这里到底有什么不对?