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.

Upvotes: 1

Views: 100

Answers (1)

fubar
fubar

Reputation: 17378

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

Upvotes: 2

Related Questions