2

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

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

前任:

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

4 回答 4

4

只需使用 ReflectionMethods 检查链接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";
        }
于 2013-08-13T05:45:45.447 回答
1

尝试这个,

$check = new ReflectionMethod('class', 'method');
if($check->isPublic()){
    echo "public";
} elseif($check->isPrivate()){
    echo "private";
} else{
    echo "protected";
}
于 2013-08-13T05:47:08.670 回答
0

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

于 2013-08-13T05:43:15.467 回答
0

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

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

于 2013-08-13T05:51:08.880 回答