views:

30

answers:

1

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 possible? I tried something like echo ($params[0])->name but I get an error.

+1  A: 

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
Pascal MARTIN
doh. I swear I tried that intuitively, but I guess I had another error.
scottm
must have expected parantheses NOT to break your code. same here.
aib