简体   繁体   中英

How do I get all the function names in the class? (Without extends)

I am trying to get all the function names in a class with PHP. Using the get_class_methods() method. But it writes in the function names of the class I extend. what I want is just the function names of the class I'm calling.

Example :

class a {
   public function index() {
      //...
   }
}

class b extends a {
   public function indexb() {
     //...
   }
}

print_r(get_class_methods(new b()));

Output :

array([0] => index, [1] => indexb);

(index) I do not want this area.

Best Regards.

If you only want the methods for the child class, you're going to need to use Reflection .

<?php

class A
{
    public function index() {}
}

class B extends A
{
    public function indexB() {}
}

$reflection = new ReflectionClass('B');

$methods = array_reduce($reflection->getMethods(), function($methods, $method) {
    if ($method->class == 'B') $methods[] = $method->name;

    return $methods;
}, []);

print_r($methods);

And a working example: https://3v4l.org/veTeQ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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