PHP - 如何捕获'试图获取非对象的属性'错误

时间:2022-04-03 15:42:46

I am trying to catch 'Trying to get property of non-object' error with a try/catch statement but it is failing, I still get a PHP error. I am using as:

我试图通过try / catch语句捕获'试图获取非对象的属性'错误但它失败了,我仍然得到PHP错误。我用作:

try{
  $id = Model()->find('id=1')->id;
}catch(Exception $e){
  echo 'failed';
}

My find function returns an object (Active Record) and I can access the id column as shown via object prop.

我的find函数返回一个对象(Active Record),我可以通过object prop访问id列。

However, it will be null object if AR is not found. I thought the try statement would catch this. A work around for myself would be to use an isset(). But I am confused as to why the try statement does not accept and catch this error.

但是,如果未找到AR,它将为null对象。我以为try语句会抓住这个。我自己的解决方法是使用isset()。但我很困惑为什么try语句不接受并捕获此错误。

2 个解决方案

#1


19  

try..catch works on thrown exceptions. Errors are not exceptions. You can silence errors, but please don't do that. Instead, properly check what you're getting:

try..catch适用于抛出的异常。错误不是例外。你可以沉默错误,但请不要这样做。相反,请正确检查您获得的内容:

$result = Model()->find('id=1');
if ($result) {
    $id = $result->id;
} else {
    // handle this situation
}

#2


5  

The model needs to be able to throw an exception.

该模型需要能够抛出异常。

Here's what your model might look like:

这是您的模型可能的样子:

class Model{

   public function find($id){
      $result = //do stuff to find by id

      if (!isset($result)){
          throw new Exception("No result was found for id:$id");
      }
      return $result

   }
}

Then you would use your try/catch block:

然后你会使用你的try / catch块:

try{  

    $id = Model()->find('id=1')->id;

}catch(Exception $e){
    echo 'failed';
}

However, exceptions should only be thrown under "exceptional" circumstances. I don't think using exceptions to direct program flow is the right way to go about it.

但是,例外情况只应在“特殊”情况下抛出。我不认为使用例外来指导程序流是正确的方法。

Having said that, if returning a NULL when you attempt to retrieve the ID property is an exceptional situation, then exceptions are certainly suitable.

话虽如此,如果在尝试检索ID属性时返回NULL是一种特殊情况,那么异常肯定是合适的。

#1


19  

try..catch works on thrown exceptions. Errors are not exceptions. You can silence errors, but please don't do that. Instead, properly check what you're getting:

try..catch适用于抛出的异常。错误不是例外。你可以沉默错误,但请不要这样做。相反,请正确检查您获得的内容:

$result = Model()->find('id=1');
if ($result) {
    $id = $result->id;
} else {
    // handle this situation
}

#2


5  

The model needs to be able to throw an exception.

该模型需要能够抛出异常。

Here's what your model might look like:

这是您的模型可能的样子:

class Model{

   public function find($id){
      $result = //do stuff to find by id

      if (!isset($result)){
          throw new Exception("No result was found for id:$id");
      }
      return $result

   }
}

Then you would use your try/catch block:

然后你会使用你的try / catch块:

try{  

    $id = Model()->find('id=1')->id;

}catch(Exception $e){
    echo 'failed';
}

However, exceptions should only be thrown under "exceptional" circumstances. I don't think using exceptions to direct program flow is the right way to go about it.

但是,例外情况只应在“特殊”情况下抛出。我不认为使用例外来指导程序流是正确的方法。

Having said that, if returning a NULL when you attempt to retrieve the ID property is an exceptional situation, then exceptions are certainly suitable.

话虽如此,如果在尝试检索ID属性时返回NULL是一种特殊情况,那么异常肯定是合适的。