2

Is any way to check method type in php if it is public, private or protected?

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"

Ex:

if (method_type ('Get_user') == 'private'){
    header ("location: ./")
}
Ayman Hussein
  • 3,817
  • 7
  • 28
  • 48
  • 3
    possible duplicate of [Checking method visibility in PHP](http://stackoverflow.com/questions/2981622/checking-method-visibility-in-php) –  Aug 13 '13 at 05:41
  • You should not confuse (or mix) access rights with the visibility of methods. Your program design should not be dependent on what users can access. ---------- For the record: it [How to check if a function is public or protected in PHP](http://stackoverflow.com/questions/4160901/how-to-check-if-a-function-is-public-or-protected-in-php) – Langdi Aug 13 '13 at 05:42

4 Answers4

4

Simply use ReflectionMethods Check Link 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";
        }
1

try this,

$check = new ReflectionMethod('class', 'method');
if($check->isPublic()){
    echo "public";
} elseif($check->isPrivate()){
    echo "private";
} else{
    echo "protected";
}
Janith Chinthana
  • 3,792
  • 2
  • 27
  • 54
0

you could use Reflection class, such as ReflectionMethod::isPrivate

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0

You could try using ReflectionMethod, check the following link for more info on it: http://php.net/manual/en/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

raul
  • 111
  • 3