2015-09-26 68 views
1

我想知道是否有辦法檢查它的類中的方法的訪問修飾符。比如我想用笨的重映射法爲帳戶系統:檢查方法的訪問修飾符

public function _remap($method, $params = array()){ 

    if($this->validation->isValidActiveSession()){ 

     if(method_exists($this, $method)) 
      call_user_func_array(array($this, $method), $params); 
     else 
      show_404(); 

    }else{ 

     redirect('login'); 

    } 

} 

如果我不能找到一個用戶的有效會話,他被拒絕。我還想確保,擁有適當會話的用戶只能調用公共方法。不幸的是method_exists()返回true,無論該方法是公有還是私有。

我已經有這一特定問題的解決方案:

  • 使用陣列,在每一個公共方法

  • 不使用重映射和驗證會話存儲爲有效用戶

可用的方法

但它感覺不方便,所以我只是在尋找一個'發燒友'的解決方案。

回答

2

使用Reflectionmethod

$reflection = new ReflectionMethod($this, $method); 
    if ($reflection->isPublic()) { 
     echo "Public method"; 
    } 
    if ($reflection->isPrivate()) { 
     echo "Private method"; 
    } 
    if ($reflection->isProtected()) { 
     echo "Protected method"; 
    }