Checking method visibility in PHP

Is there a way to check if a class method has been declared as private or public?

I am working on a controller where the URL maps to methods in the class, and I only want to run the methods if they are defined as public.

+6
visibility methods oop php class
source share
3 answers
+8
source share

To extend Safraz Ahmed's answer (since Reflection lacks documentation), this is a quick example:

class foo { private function bar() { echo "bar"; } } $check = new ReflectionMethod('foo', 'bar'); echo $check->isPrivate(); 
+7
source share

Let's look from the other side. You do not need to know the level of visibility of the method. You need to know if you can call a method. http://lv.php.net/is_callable

 if(is_callable(array($controller, $method))){ return $controller->$method(); }else{ throw new Exception('Method is not callable'); return false; } 
+2
source share

All Articles