PHP - How to catch a 'Trying to get property of non-object' error

前端 未结 2 1362
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 10:41

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         


        
2条回答
  •  孤街浪徒
    2020-12-10 11: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:

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

提交回复
热议问题