PHP 8.4.2 Released!

get_class_methods

(PHP 4, PHP 5, PHP 7, PHP 8)

get_class_methodsObtém o nome dos métodos da classe

Descrição

get_class_methods(object|string $object_or_class): array

Obtém o nome dos métodos da classe.

Parâmetros

object_or_class

O nome da classe ou a instância do objeto

Valor Retornado

Retorna um array de nome de métodos definidos para a classe especificada por object_or_class.

Registro de Alterações

Versão Descrição
8.0.0 O parâmetro object_or_class agora aceita apenas objetos ou nomes de classe válidos.

Exemplos

Exemplo #1 Exemplo de get_class_methods()

<?php

class minhaclasse {
// construtor
function __construct()
{
return(
true);
}

// método 1
function minhafuncao1()
{
return(
true);
}

// método 2
function minhafuncao2()
{
return(
true);
}
}

$metodos_da_classe = get_class_methods('minhaclasse');
// ou
$metodos_da_classe = get_class_methods(new minhaclasse());

foreach (
$metodos_da_classe as $nome_do_metodo) {
echo
"$nome_do_metodo\n";
}

?>

O exemplo acima produzirá:

__construct
minhafuncao1
minhafuncao2

Veja Também

adicione uma nota

Notas Enviadas por Usuários (em inglês) 4 notes

up
60
fschmengler at sgh-it dot eu
14 years ago
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)
up
6
polarglow06 at gmail dot com
9 years ago
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();
up
4
php at stock-consulting dot com
17 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 :-)
up
0
faizanakram99+php at gmail dot com
9 days ago
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
To Top