0

I have a column of type array. It stores pointers to other objects. So in my loop I have something like this:

foreach ($artworkArr as $a) {
    $a->fetch();
    $artworkName[] = $a->get('title');
}

Problem here is, fetch immediately throws an exception if the pointer points to an invalid object.

How can I check if the object exists before calling the fetch method?

1
  • Try using try/catch to catch these exceptions Commented Jun 17, 2015 at 8:05

1 Answer 1

1

Just try with:

foreach ($artworkArr as $a) {
    if ($a instanceof ExpectedClass) {
        $a->fetch();
        $artworkName[] = $a->get('title');
    }
}

Or if you don't know the ExpectedClass, you can use method_exists on the $a object:

method_exists($a, 'fetch')
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.