没想到的''错误休息api php

时间:2016-10-07 08:58:23

标签: php rest api

public function processApi(){       
  $func = strtolower(trim(str_replace("/","",$_SERVER['REQUEST_URI'])));        //echo $func;

   if((int)method_exists($this,$func) > 0)          
   $this->$func();      
   else             
   $this->response('',404); 

   echo '404';      // If the method not exist with in this class, response would be "Page not found".  
}

1 个答案:

答案 0 :(得分:0)

您应该使用此代码

    public function processApi(){       
         $func =  strtolower(trim(str_replace("/","",$_SERVER['REQUEST_URI'])));       
         if(method_exists(get_called_class(), $func))
            {
                 $this->$func();
            }else{
                $this->response('method not exists',404);  
            }
  } 

此方法获取类名

  

get_called_class()

在方法中设置类名返回true是否存在false方法

  

method_exists(get_called_class(),$ func)

试试这段代码