get_class_methods
(PHP 4, PHP 5)
get_class_methods — Gets the class methods' names
Parameters
-
class_name
-
The class name or an object instance
Return Values
Returns an array of method names defined for the class specified by
class_name
. In case of an error, it returns NULL
.
Changelog
Version | Description |
---|---|
5.0.0 | As of PHP 5, this function returns the name of the methods as they were declared (case-sensitive). In PHP 4 they were lowercased. |
4.0.6 | The ability of specifying the object itself has been added. |
Examples
Example #1 get_class_methods() example
<?php
class myclass {
// constructor
function myclass()
{
return(true);
}
// method 1
function myfunc1()
{
return(true);
}
// method 2
function myfunc2()
{
return(true);
}
}
$class_methods = get_class_methods('myclass');
// or
$class_methods = get_class_methods(new myclass());
foreach ($class_methods as $method_name) {
echo "$method_name\n";
}
?>
The above example will output:
myclass 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
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Функции работы с классами и объектами
- __autoload
- call_user_method_array
- call_user_method
- class_alias
- class_exists
- get_called_class
- get_class_methods
- get_class_vars
- get_class
- get_declared_classes
- get_declared_interfaces
- get_declared_traits
- get_object_vars
- get_parent_class
- interface_exists
- is_a
- is_subclass_of
- method_exists
- property_exists
- trait_exists
Коментарии
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 :-)
It should be noted that the returned methods are dependant on the current scope. See this example:
<?php
class C
{
private function privateMethod()
{
}
public function publicMethod()
{
}
public function __construct()
{
echo '$this:';
var_dump(get_class_methods($this));
echo 'C (inside class):';
var_dump(get_class_methods('C'));
}
}
$c = new C;
echo '$c:';
var_dump(get_class_methods($c));
echo 'C (outside class):';
var_dump(get_class_methods('C'));
?>
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)
I have created a very simple test runner using this function
function get_bar($text) {
$bar = "";
for($i=1; $i<=strlen($text); $i++) {
$bar .= "=";
}
return $bar;
}
class Tester {
function __construct() {
$this->run_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();
It is worth noting that get_class_methods($closure) doesn't return __invoke method even though it exists and is documented too.
See https://3v4l.org/VKjAF