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{
$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.
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.
try..catchworks on thrown exceptions. Errors are not exceptions. You can silence errors, but please don’t do that. Instead, properly check what you’re getting: