2010-02-17 57 views
0

我想使用PHP的反射功能從方法中檢索參數名稱列表。我有這樣一個類:如何從類變量數組中訪問類成員?

class TestClass { 
    public function method($id, $person, $someotherparam) { 
     return; 
    } 
} 

我可以用這樣的代碼獲取列表:

$r = new ReflectionClass('TestClass'); 

$methods = $r->getMethods(); 

foreach($methods as $method) { 
    $params = $method->getParameters(); 
    $p = $params[0]; // how can I combine this and the next line? 
    echo $p->name; 

我想知道如何從數組訪問類的成員,因此我不必須做一個任務。這可能嗎?我嘗試了一些像echo ($params[0])->name但我得到一個錯誤。由單一的一個

$p = $params[0]; // how can I combine this and the next line? 
echo $p->name; 

回答

1

可以取代這兩條線路

echo $params[0]->name; 

即不需要任何括號這裏。


但你不能使用這種語法:

($params[0])->name 

它會給你一個

Parse error: syntax error, unexpected T_OBJECT_OPERATOR 
+0

衛生署。我發誓我直覺地嘗試過,但我想我有另一個錯誤。 – scottm 2010-02-17 21:30:17

+0

必須有預期的禁忌不要打破你的代碼。同樣在這裏。 – aib 2010-02-18 02:28:33

相關問題