php - Check if a field/propertry exists in a variable of type object -
i using zend_search_lucene, index website. site indexes not entirely similar. have, few fields, , have many fields. trying create similar index through different types of table, that's why encountering kind of error.
now, when display result. call fields, not present in result generates error. tried check isset seems totally skips row.
foreach ($hits $hit) { $content .= '<div class="searchresult">'; $content .= '<h2>'; $title = array(); if(isset($hit -> name)) $title[] = $hit -> name; if(isset($hit -> title)) $title[] = $hit -> title; // part fatal error. $content .= implode(" » ",$title); $content .= '</h2>'; $content .= '<p>'.$this->content.'</p>'; $content .= '</div>'; } how check if there such $hit -> name present in $hit
the problem experiencing very specific , has zend_lucene_search implementation, not field/property exist check in general.
in loop, $hit object of class zend_search_lucene_search_queryhit. when write expression $hit->name, object calls magic __get function give "virtual property" named name. magic function throws exception if value supplied not exist.
normally, when class implements __get convenience should implement __isset convenience (otherwise cannot use isset on such virtual properties, have found out hard way). since particular class not implement __isset imho should, never able name "property" blindly without triggering exception if relevant data not exist.
property_exists , other forms of reflection not since not talking real property here.
the proper way solve little roundabout:
$title = array(); $names = $hit->getdocument()->getfieldnames(); if(in_array('name', $names)) $title[] = $hit -> name; if(in_array('title',$names)) $title[] = $hit -> title; all said, 'd consider bug in zf , file report, asking __isset magic method implemented appropriately on types should be.
Comments
Post a Comment