NiNjA_HacKeR
NiNjA_HacKeR

Reputation: 1

Error handling in php functions without crashes

I want to handle an error in my PHP function without crashing my whole script.

First it pushes this error:

Notice: Trying to get property of non-object in E:\amir\1magazine\tools\xampp\htdocs\CairoHash\assests\plugins\content_handling.php on line 74

I know what the error is for, but I want to continue without crashing my script.

I tried if but it didn't work as I hoped.

$count = count($selector);
$order = substr($order, 0 , -1);
switch ($count)
{
    case 1:
        $i = $html->find($selector);                 
        break;
    case 2:
        $i = $html->find($selector[0],$selector[1]);
        break;
    case 3:
        /*  ln 74 ->>> */      $i = $html->find($selector[0],$selector[1])->$selector[2];
        break;
}
if(isset($i))
{
    return $i;
}

This code is supposed to get the $i variable but I need to say if it didn't work then don't crash.

Upvotes: 0

Views: 150

Answers (1)

Don't Panic
Don't Panic

Reputation: 41810

You just need to check earlier if something is set.

Notice: Trying to get property of non-object

on line 74 means that find($selector[0],$selector[1]) is not returning an object. So you can do your checks there instead.

case 3:           
    $x = $html->find($selector[0],$selector[1]);
    if ($x) $i = $x->$selector[2];
    break;

That should fix this specific error, but you may encounter the same problem for other cases, so you should verify that find() was successful in every case before using its result.

Upvotes: 1

Related Questions