PHP how to list out all public functions of class

后端 未结 4 1606
悲&欢浪女
悲&欢浪女 2020-12-30 02:32

I\'ve heard of get_class_methods() but is there a way in PHP to gather an array of all of the public methods from a particular class?

相关标签:
4条回答
  • 2020-12-30 03:16

    Yes you can, take a look at the reflection classes / methods.

    http://php.net/manual/en/book.reflection.php and http://www.php.net/manual/en/reflectionclass.getmethods.php

    $class = new ReflectionClass('Apple');
    $methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
    var_dump($methods);
    
    0 讨论(0)
  • 2020-12-30 03:18

    Have you try this way?

    $class_methods = get_class_methods(new myclass());
    
    foreach ($class_methods as $method_name) {
        echo "$method_name\n";
    }
    
    0 讨论(0)
  • After getting all the methods with get_class_methods($theClass) you can loop through them with something like this:

    foreach ($methods as $method) {
        $reflect = new ReflectionMethod($theClass, $method);
        if ($reflect->isPublic()) {
        }
    }
    
    0 讨论(0)
  • 2020-12-30 03:32

    As get_class_methods() is scope-sensitive, you can get all the public methods of a class just by calling the function from outside the class' scope:

    So, take this class:

    class Foo {
        private function bar() {
            var_dump(get_class_methods($this));
        }
    
        public function baz() {}
    
        public function __construct() {
            $this->bar();
        }
    }
    

    var_dump(get_class_methods('Foo')); will output the following:

    array
      0 => string 'baz' (length=3)
      1 => string '__construct' (length=11)
    

    While a call from inside the scope of the class (new Foo;) would return:

    array
      0 => string 'bar' (length=3)
      1 => string 'baz' (length=3)
      2 => string '__construct' (length=11)
    
    0 讨论(0)
提交回复
热议问题