scottm
scottm

Reputation: 28701

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

Upvotes: 0

Views: 536

Answers (1)

Pascal MARTIN
Pascal MARTIN

Reputation: 401002

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

Upvotes: 1

Related Questions