PHP Conference Japan 2024

get_class_methods

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

get_class_methods取得類別方法的名稱

說明

get_class_methods(物件|字串 $object_or_class): 陣列

取得類別方法的名稱。

參數

object_or_class

類別名稱或物件實例

回傳值

回傳一個陣列,其中包含由 object_or_class 指定的類別所定義的方法名稱。

更新日誌

版本 說明
8.0.0 現在 object_or_class 參數只接受物件或有效的類別名稱。

範例

範例 #1 get_class_methods() 範例

<?php

class myclass {
// 建構子
function __construct()
{
return(
true);
}

// 方法 1
function myfunc1()
{
return(
true);
}

// 方法 2
function myfunc2()
{
return(
true);
}
}

$class_methods = get_class_methods('myclass');
// 或
$class_methods = get_class_methods(new myclass());

foreach (
$class_methods as $method_name) {
echo
"$method_name\n";
}

?>

上述範例將輸出

__construct
myfunc1
myfunc2

另請參考

新增註釋

使用者貢獻的註釋 13 則註釋

fschmengler at sgh-it dot eu
14 年前
應該注意的是,返回的方法取決於目前的範圍。請參考以下範例

<?php
class C
{
private function
privateMethod()
{

}
public function
publicMethod()
{

}
public function
__construct()
{
echo
'$this:';
var_dump(get_class_methods($this));
echo
'C (類別內部):';
var_dump(get_class_methods('C'));
}
}
$c = new C;
echo
'$c:';
var_dump(get_class_methods($c));
echo
'C (類別外部):';
var_dump(get_class_methods('C'));
?>

輸出

$this
陣列
0 => 字串 'privateMethod' (長度=13)
1 => 字串 'publicMethod' (長度=12)
2 => 字串 '__construct' (長度=11)

C (類別內部)
陣列
0 => 字串 'privateMethod' (長度=13)
1 => 字串 'publicMethod' (長度=12)
2 => 字串 '__construct' (長度=11)

$c
陣列
0 => 字串 'publicMethod' (長度=12)
1 => 字串 '__construct' (長度=11)

C (類別外部)
陣列
0 => 字串 'publicMethod' (長度=12)
1 => 字串 '__construct' (長度=11)
gk at proliberty dot com
21 年前
需要注意的是,`get_class_methods($class)` 不僅會返回 `$class` 定義的方法,還會返回繼承的方法。

似乎沒有任何 PHP 函式可以判斷哪些方法是繼承的,哪些是由類別明確定義的。
onesimus at cox dot net
20 年前
這個函式只會返回您指定物件的方法。它會去除繼承的方法。

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);
}
matt at zevi dot net
22 年前
僅適用於 Win32

在此值得一提的是,您無法取得由內建 'COM' 類別建立的物件的方法。例如,以下程式碼無法運作:

$word = new COM('Word.Application');
$methods = get_class_methods(get_class($word));
print_r($methods);

Matt
jazepstein at greenash dot net dot au
19 年前
在 PHP4 中,這個函式會將其返回值轉換為小寫;但在 PHP5 中,它會保留返回值的原始大小寫。這會在嘗試編寫動態呼叫類別方法,且需同時在 PHP4 和 PHP5 中運作的程式碼時造成嚴重問題。以下程式碼片段顯示一種與兩個版本相容的方法:

<?php
// 範例變數 - 這些在實際應用程式中會是動態的。
$className = 'SomeClass';
$methodName= 'someMethod';
$args = array('arg1', 'arg2');

// 使用 array_map() 和 strtolower() 將 get_class_methods() 返回的所有值轉換為小寫。PHP4 本來就會這樣做 - 現在無論使用哪個 PHP 版本都會發生。
$classMethods = array_map(strtolower, get_class_methods($className));

// in_array() 只能處理陣列。
if (!$classMethods) {
$classMethods = array();
}

if (
in_array(strtolower($methodName), $classMethods)) {
// 呼叫某些方法
return call_user_func_array(array($className, $methodName), $args);
}
?>
Oli Filth
19 年前
作為 onesimus 程式碼的延伸,用於尋找繼承的方法,在 PHP 5 中,您可以使用 Reflection API 來找出哪些方法被覆寫。

例如:

<?php
函數 get_overriden_methods($class)
{
$rClass = new ReflectionClass($class);
$array = NULL;

foreach (
$rClass->getMethods() as $rMethod)
{
try
{
// 嘗試在父類別中尋找方法
new ReflectionMethod($rClass->getParentClass()->getName(),
$rMethod->getName());
// 檢查方法是否在此類別中明確定義
if ($rMethod->getDeclaringClass()->getName()
==
$rClass->getName())
{
// 如果是,則它已被覆寫,因此添加到陣列中
$array[] .= $rMethod->getName();
}
}
catch (
exception $e)
{
/* 不在父類別中! */ }
}

return
$array;
}
?>
polarglow06 at gmail dot com
9 年前
我使用這個函數創建了一個非常簡單的測試執行器

function get_bar($text) {
$bar = "";
for($i=1; $i<=strlen($text); $i++) {
$bar .= "=";
}
return $bar;
}
class Tester {
function __construct() {
$this->run_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");
}
}
}

現在您只需要編寫您的測試類別,並以 'test' 作為測試方法的前綴,然後只需實例化您的測試類別的物件,所有這些測試方法就會自動運行
缺點是:您的測試方法不能接受任何參數

一個例子
require '../autoload.php';
register_autoload_paths(realpath('./'));

class Test_Test extends Tester {
function test_something() {
print("方法已執行");
}
function testAnotherThing() {
print("另一個測試方法");
}
}

$Test = new Test_Test();
aldo at cerca dot com
22 年前
如果您使用 "get_class_methods" 來檢查類別中是否存在方法,請記住該函數會返回類別方法的小寫名稱

class classPippo
{
function DummyFunct()
{
// 不做任何事...
}
}

$aClassMethods = get_class_methods(classPippo);

$sMethodName = 'DummyFunct';

// 這不起作用...

if (in_array($sMethodName, $aClassMethods))
classPippo::DummyFunct();

// 這起作用...

if (in_array(strtolower($sMethodName), $aClassMethods))
classPippo::DummyFunct();
php at stock-consulting dot com
17 年前
請注意,這個函式會同時返回類別方法和實例方法(在 PHP 中,「類別方法」稱為「靜態方法」)。對於深入了解物件導向術語的人來說,這算是一個小「陷阱」 :-)
kabatak
19 年前
在 PHP4 中,如果您需要以原始大小寫取得 get_class_methods 的結果,您可以使用我建立的這個簡單函式。

// 注意:此函式假設您在一個檔案中只有一個類別

$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;
}
BoD
18 年前
!僅關於 PHP5!

如果您想取得類別中的所有方法/函式,您可以使用 get_class_methods 函式來完成。
<?php
$arrMethodNames
= get_class_methods ( $ClassNameOrObject);
?>
然而,在 PHP5 中,這個函式的缺點是,如果您從另一個類別/物件的上下文呼叫此方法,您將無法取得類別/物件的 protected 和 private 方法。

如果您想在另一個類別中取得指定類別的所有方法,您應該使用 PHP5 的 Reflection API。以下程式碼允許從其(抽象)基底類別的衍生類別中擷取所有方法。

在這個例子中,您需要從衍生類別的建構函式中呼叫基底類別的建構函式,以便讓基底類別知道衍生類別的名稱。使用「__CLASS__」定義將目前類別的類別名稱傳遞給其基底類別。

<?php

// 基底類別 - 抽象類別
abstract class A {

// ReflectionMethod 物件陣列
// 於實例化時設定
// 派生類別不需要知道這個陣列
private $arrMethods;

// 建構子,必須從派生類別建構子呼叫
protected function __construct ( $strDerivedClassName ) {
$oRefl = new ReflectionClass ( $strDerivedClassName );
if (
is_object($oRefl) ) {
$this->arrMethods = $oRefl->getMethods();
}
}

// 一些抽象函式
abstract protected function D ();

// 一些私有函式
private function E() {
}

// 印出所有類別/物件方法的方法
// 必須可從派生類別呼叫
// 如果僅供內部類別使用,可以設為 protected
public function PrintAllMethods () {
foreach (
$this->arrMethods as $curReflectionMethod ) {
echo
$curReflectionMethod->getName()."<br>";
}
}
}


// 派生類別
class B extends A {

// 此類別的建構子
// 必須呼叫基底類別建構子
public function __construct () {
// 呼叫基底類別建構子
parent::__construct(__CLASS__);
}


// 一些公有函式
public function A () {
}

// 一些保護函式
protected function B () {
}

// 一些私有函式
private function C() {
}

// 實作了基底類別的一些抽象方法
protected function D () {
}
}


// 建立新的 B 物件
$b = new B();
// 印出此物件/類別的所有方法
$b->PrintAllMethods();
?>

在此範例中,輸出將會是

__construct
A
B
C
D
E
PrintAllMethods

如您所見,這些是 B 類別的所有方法,也是 A 類別的所有方法,並按照它們的宣告順序排列。請注意,只顯示一個 "__construct" 方法和一個 "D" 方法。這是由於 PHP 中的覆寫 (__construct) 和實作 (D) 機制。

在此範例中,任何進一步的方法處理方法都應該在基底類別中實作,因為這些方法在派生類別中也將可用。只要確保您為這些額外的方法使用正確的存取修飾詞 (private、protected、public) 即可。

BoD
iarias at loyalia dot es
6 年前
如果您只需要類別宣告的方法,而不是父類別的方法,您可以執行以下操作:

$declared_methods = array_diff(get_class_methods($class), get_class_methods(get_parent_class($class)));
epowell at removethis dot visi dot com
20 年前
我已經找到了如何使用 Reflection API 解決我在下面描述的問題。

<?
/* 傳入類別的名稱,而不是宣告的處理器 */
function get_public_methods($className) {
/* 初始化返回陣列 */
$returnArray = array();

/* 迭代類別中的每個方法 */
foreach (get_class_methods($className) as $method) {

/* 取得類別方法的 reflection 物件 */
$reflect = new ReflectionMethod($className, $method);

/* 對於私有方法,使用 isPrivate()。對於受保護方法,使用 isProtected() */
/* 更多定義請參閱 Reflection API 文件 */
if($reflect->isPublic()) {
/* 這個方法是我們要找的,將其推入返回陣列 */
array_push($returnArray,$method);
}
}

} // 加上缺少的括號
/* 將陣列返回給呼叫者 */
}
?>
To Top