2016-10-07 55 views
-4
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". 
} 

回答

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()

在方法的返回真假方法

集类名存在与否

method_exists(get_called_class(),$ FUNC)

试试这个代码

+0

感谢回答。 ,但仍然错误。 错误是:意外'' – jerry

+0

如果您使用MVC结构,所以得到错误,但使用核心oops没有得到error.so如果您将使用MVC尝试$ this-> router-> fetch_method();替换$ _SERVER ['REQUEST_URI'] –

+0

我使用了核心代码。 – jerry