【问题标题】:check method type if it is public, private or protected检查方法类型是公共的、私有的还是受保护的
【发布时间】:2013-08-14 14:47:15
【问题描述】:

有什么方法可以检查 php 中的方法类型是公共的、私有的还是受保护的?

我尝试了什么: 我有课程,它有方法我可以把这个方法放在 url 和 grt 页面中,所以我需要一种方法,如果用户将私有方法放在 url 中,那么用户会得到一个错误页面,例如“访问被拒绝”

例如:

if (method_type ('Get_user') == 'private'){
    header ("location: ./")
}

【问题讨论】:

标签: php oop


【解决方案1】:

您可以使用Reflection 类,例如 ReflectionMethod::isPrivate

【讨论】:

    【解决方案2】:

    只需使用反射方法 查看链接http://www.php.net/manual/en/class.reflectionmethod.php

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

    【讨论】:

      【解决方案3】:

      试试这个,

      $check = new ReflectionMethod('class', 'method');
      if($check->isPublic()){
          echo "public";
      } elseif($check->isPrivate()){
          echo "private";
      } else{
          echo "protected";
      }
      

      【讨论】:

        【解决方案4】:

        您可以尝试使用 ReflectionMethod,查看以下链接了解更多信息: http://php.net/manual/en/class.reflectionmethod.php

        您也可以尝试使用 is_callable ,但这与范围有关,因此它会根据您所在的课程产生不同的结果。您可以在此处查看: http://www.php.net/manual/en/function.is-callable.php

        【讨论】:

          猜你喜欢
          • 2011-05-29
          • 2013-06-24
          • 2016-03-27
          • 2010-10-21
          • 2012-09-09
          • 2012-01-16
          • 1970-01-01
          • 1970-01-01
          • 2011-07-21
          相关资源
          最近更新 更多