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?
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')