且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查对象方法是否存在?

更新时间:2023-11-25 23:09:52

您可以使用函数method_exists来检查该方法是否存在于类中.例如

You can use the function method_exists to check if the method is existing in a class or not. for example

if(method_exists('CLASS_NAME', 'METHOD_NAME') ) 
   echo "it does exist!"; 
else 
   echo "nope, it is not there...";

在您的代码中尝试

if(method_exists($item, 'getProductgroup')){
$productgroupValidation = 0;
if(method_exists($item->getProductgroup(), 'getUuid'))
{
   $productgroupId = $item->getProductgroup()->getUuid();
   foreach($dataField->getProductgroup() as $productgroup)
   {
        $fieldProductgroup = $productgroup->getUuid();
        if($productgroupId==$fieldProductgroup){
            $productgroupValidation = 1;
        }
    }
 }
}