Hướng dẫn php get class methods

[PHP 4, PHP 5, PHP 7, PHP 8]

get_class_methodsGets the class methods' names

Description

get_class_methods[object|string $object_or_class]: array

Parameters

object_or_class

The class name or an object instance

Return Values

Returns an array of method names defined for the class specified by object_or_class.

Changelog

VersionDescription
8.0.0 The object_or_class parameter now only accepts objects or valid class names.

Examples

Example #1 get_class_methods[] example

The above example will output:

__construct
myfunc1
myfunc2

See Also

  • get_class[] - Returns the name of the class of an object
  • get_class_vars[] - Get the default properties of the class
  • get_object_vars[] - Gets the properties of the given object

fschmengler at sgh-it dot eu

12 years ago

It should be noted that the returned methods are dependant on the current scope. See this example:



Output:

$this:
array
  0 => string 'privateMethod' [length=13]
  1 => string 'publicMethod' [length=12]
  2 => string '__construct' [length=11]

C [inside class]:
array
  0 => string 'privateMethod' [length=13]
  1 => string 'publicMethod' [length=12]
  2 => string '__construct' [length=11]

$c:
array
  0 => string 'publicMethod' [length=12]
  1 => string '__construct' [length=11]

C [outside class]:
array
  0 => string 'publicMethod' [length=12]
  1 => string '__construct' [length=11]

gk at proliberty dot com

19 years ago

It is important to note that get_class_methods[$class] returns not only methods defined by $class but also the inherited methods.

There does not appear to be any PHP function to determine which methods are inherited and which are defined explicitly by a class.

matt at zevi dot net

20 years ago

Win32 only:

It's probably worth noting here that you can't get the methods of an object created by the built-in 'COM' class. ie - this won't work:

$word = new COM['Word.Application'];
$methods = get_class_methods[get_class[$word]];
print_r[$methods];

Matt

jazepstein at greenash dot net dot au

16 years ago

In PHP4, this function converts its return values to lowercase; but in PHP5, it leaves the return values in their original case. This can cause serious problems when trying to write code that dynamically calls a class method, and that works in both PHP4 and PHP5. This code snippet shows one way of achieving compatibility with both versions:

onesimus at cox dot net

18 years ago

This function will return only the methods for the object you indicate. It will strip out the inherited methods.

function get_this_class_methods[$class]{
    $array1 = get_class_methods[$class];
    if[$parent_class = get_parent_class[$class]]{
        $array2 = get_class_methods[$parent_class];
        $array3 = array_diff[$array1, $array2];
    }else{
        $array3 = $array1;
    }
    return[$array3];
}

polarglow06 at gmail dot com

6 years ago

I have created a very simple test runner using this function

function get_bar[$text] {
    $bar = "";
    for[$i=1; $irun_tests[];
    }
    // run the tests
    function run_tests[] {
        print["Tester by Minhajul Anwar \n"];
        $class = get_class[$this];
        $test_methods = preg_grep['/^test/', get_class_methods[$this]];
        foreach[$test_methods as $method] {
            $start_rep = "test: $class::$method";
            $bar = get_bar[$start_rep];
            print["\n$start_rep\n$bar\n"];
            $this->$method[];
            print["\n"];
        }
    }
}

now you just need to write your test class with tegst methods prefixed by 'test', and then just instantiate object of that test class of your, all those tests methods will get run automatically
The drawback is: your test methods must not accept any arguments

an example:
require '../autoload.php';
register_autoload_paths[realpath['./']];

class Test_Test extends Tester {
    function test_something[] {
        print["method got executed"];
    }
    function testAnotherThing[] {
    print["another test method"];
    }
}

$Test = new Test_Test[];

Oli Filth

17 years ago

As an extension to onesimus's code below for finding inherited methods, in PHP 5 you can use the Reflection API to find which of these are overriden.

e.g.

kabatak

17 years ago

In PHP4, if you need to get_class_methods in their original case. You can use this simple function I created.

// Note: this function assumes that you only have 1 class in 1 file

$file = "path/to/myclass.php"

function file_get_class_methods [$file]
{
    $arr = file[$file];
    foreach [$arr as $line]
    {
        if [ereg ['function [[_A-Za-z0-9]+]', $line, $regs]]
            $arr_methods[] = $regs[1];
    }
    return $arr_methods;
}

php at stock-consulting dot com

15 years ago

Note that this function will answer both class AND instance methods ["class methods" are called "static" in PHP]. Sort of a little "trap" for people who have in-depth experience with the OO terminology :-]

aldo at cerca dot com

20 years ago

If you use "get_class_methods" to check if a Method is in a Class remember that the function return lowered name of class methods:

class classPippo
{
        function DummyFunct[]
        {
                // Do nothing...
        }
}

$aClassMethods = get_class_methods[classPippo];

$sMethodName = 'DummyFunct';

// This not work...

        if [in_array[$sMethodName, $aClassMethods]]
        classPippo::DummyFunct[];

// This work...

        if [in_array[strtolower[$sMethodName], $aClassMethods]]
        classPippo::DummyFunct[];

BoD

16 years ago

!Concerning PHP5 Only!

If you want to get all methods/functions from a class you can do this with the get_class_methods function.

However the drawback on this function in PHP5 is that you will NOT receive protected and private methods of a class/object if you are calling the method from another class/object context.

If you want to receive all methods of a given Class in another Class you should use the PHP5 Reflection API. The following source allows to retrieve all methods from a derived class in its [abstract] Base Class.

In the example you need to call the base constructor from the derived classes constructor in order to let the base class know the name of the derived class. Use the "__CLASS__" definition for passing the classname of current class to its base class.

Chủ Đề