2015-09-26 112 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"; 
    }