Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only declared methods of a class in PHP

Tags:

oop

php

cakephp

Hello I need to get only the methods declared in a class, and not the inherited methods. I need this for cakePHP. I am getting all the controllers, loading them and retrieving the methods from those controllers. But not only are the declared methods coming, but also the inherited ones.

Is there any method to get only declared methods.

like image 909
macha Avatar asked Sep 14 '10 20:09

macha


1 Answers

You can do this (although a little more than "simple") with ReflectionClass

function getDeclaredMethods($className) {
    $reflector = new ReflectionClass($className);
    $methodNames = array();
    $lowerClassName = strtolower($className);
    foreach ($reflector->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
        if (strtolower($method->class) == $lowerClassName) {
            $methodNames[] = $method->name;
        }
    }
    return $methodNames;
}
like image 94
ircmaxell Avatar answered Sep 19 '22 01:09

ircmaxell