Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 最安全的捕获方法';对非对象';_Php_Magento - Fatal编程技术网

Php 最安全的捕获方法';对非对象';

Php 最安全的捕获方法';对非对象';,php,magento,Php,Magento,继承了一段代码,其中插件的更新已开始导致此错误 Fatal error: Call to a member function getId() on a non-object in... 其中所讨论的行是$shippingId=$order->getShippingAddress()->getId() 对周围代码的更新意味着此函数不返回任何内容,其余代码可以在$shippingId不存在的情况下运行 我需要的是一种说话的方法 if ($shippingId = $order->getShi

继承了一段代码,其中插件的更新已开始导致此错误

Fatal error: Call to a member function getId() on a non-object in...
其中所讨论的行是
$shippingId=$order->getShippingAddress()->getId()

对周围代码的更新意味着此函数不返回任何内容,其余代码可以在
$shippingId
不存在的情况下运行

我需要的是一种说话的方法

if ($shippingId = $order->getShippingAddress()->getId() WORKS)
  do_these_things()
然后,如果不出错,我会这样做,而不是继续出错:

if(isset($order->getShippingAddress())和&is_对象($order->getShippingAddress()){
/*它是一个对象,不为空*/
}否则{
/*没有*/
}

检查表达式是否不为null且具有值(以避免null指向),并检查此类表达式是否为对象。

您可以使用arielnmz提到的方法,也可以使用自定义异常处理程序

try{
if ($shippingId = $order->getShippingAddress()->getId() WORKS)
   do_these_things();
}
catch(Exception e){
//Echo out the error msg or save it on your error log stash. up to you
   $error_msg=$e->getMessage();
}
//your regular codes

使用这种方法,如果try块中有错误,错误会被catch块捕获,代码将继续正常执行模式。

不100%确定您的意思,但要检查它是否为对象use
is\u object
是,这就是我的意思!抱歉,如果我不清楚,Ari会接受@arielnmz的答案。只是为了记录:我喜欢这个想法,但是
try
块不捕捉警告或通知,只捕捉异常,所以这不起作用。在这种情况下,您需要一个新的解决方案。您可以在此处尝试此示例:。