2010-09-08 32 views
1

首先,給定一個文件,說somefile.php有沒有辦法來搜索該文件的Class以編程方式獲取類方法和屬性?

然後,一旦你有了類,有沒有辦法獲得所有的公共屬性和方法簽名?

我想創建一個方法來在飛行中記錄PHP類。

+2

以供將來參考:這種能力被稱爲反思。知道正確的條款有助於谷歌幫助你:) – jrharshath 2010-09-08 17:52:48

+1

爲什麼不簡單地使用PHPDocumentor標籤? – 2010-09-08 17:53:17

回答

2
<?php 

include("somefile.php"); 

if (class_exists("MyClass")) { 
$myclass = new ReflectionClass("MyClass"); 

// getMethods() returns an array of ReflectionMethod objects 
$methods = $myclass->getMethods(); 

foreach ($methods as $method) { 
    print $method->getName() . "():\n"; 

    // getParameters() returns an array of ReflectionParameter objects 
    $parameters = $method->getParameters(); 
    foreach ($parameters as $parm) { 
     print " " . $parm . "\n"; 
    } 
} 
}