开发者

How to access class members from an array of class variables?

I want to use PHP's reflection features to retrieve a list of parameter names from a method. I have a class like this:

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

I can get the list using code like this:

$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;

I want to know how to access the class members from the array, so I don't have to do an assignment. Is this 开发者_如何学Pythonpossible? I tried something like echo ($params[0])->name but I get an error.


you can replace these two lines :

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

by that single one :

echo $params[0]->name;

i.e. no need for any kind of parenthesis here.


But you cannot use this kind of syntax :

($params[0])->name

It'll give you a

Parse error: syntax error, unexpected T_OBJECT_OPERATOR
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜