如何从类变量数组中访问类成员?
我想使用 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
但出现错误。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将这两行 : 替换
为单行 : ,
即此处不需要任何类型的括号。
但你不能使用这种语法:
它会给你一个
you can replace these two lines :
by that single one :
i.e. no need for any kind of parenthesis here.
But you cannot use this kind of syntax :
It'll give you a