简体   繁体   English

检查方法类型是公共,私有还是受保护的

[英]check method type if it is public, private or protected

Is any way to check method type in php if it is public, private or protected? 有什么方法可以检查php中的方法类型是公共的,私有的还是受保护的?

what I tried: I have class and it have methods I can put this methods in url and grt pages so I need a way if the users put the private methods in url then the user get an error page such as "Access denied" 我试过的方法:我有类,并且有一些方法,可以将这些方法放在url和grt页面中,因此,如果用户将私有方法放在url中,则用户将获得诸如“访问被拒绝”之类的错误页面,我需要一种方法

Ex: 例如:

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

Simply use ReflectionMethods Check Link http://www.php.net/manual/en/class.reflectionmethod.php 只需使用ReflectionMethods Check Link http://www.php.net/manual/zh/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";
        }

try this, 尝试这个,

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

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

You could try using ReflectionMethod, check the following link for more info on it: http://php.net/manual/en/class.reflectionmethod.php 您可以尝试使用ReflectionMethod,请检查以下链接以获取更多信息: http : //php.net/manual/zh/class.reflectionmethod.php

Also you might try to use is_callable but that relates to scope so it will yield a different result depending on the class you're in. You can check it out here: http://www.php.net/manual/en/function.is-callable.php 另外,您可能会尝试使用is_callable,但是它与作用域有关,因此根据您所在的类,它会产生不同的结果。您可以在此处进行检查: http : //www.php.net/manual/zh/function 。是-callable.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM