0

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.

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.