【发布时间】:2026-01-30 14:20:05
【问题描述】:
我无法用谷歌搜索这个。问题;
public function processAPI() {
if (method_exists($this, $this->endpoint)) {
return $this->_response($this->{$this->endpoint}($this->args));
}
return $this->_response("No Endpoint: $this->endpoint", 404);
}
考虑$endpoint 是一个变量,$args 是一个类的数组。我们想将变量$this->{$this->endpoint}($this->args) 传递给_response() 方法。 php语法中{$this->endpoint}($this->args)是什么意思?
代码完整定义链接:http://coreymaynard.com/blog/creating-a-restful-api-with-php/
【问题讨论】:
-
如果这不是重复的,应该添加到*.com/questions/3737139/…
-
这是由于使用了variable variables - 或者在这种情况下是变量方法名称。但据我所知,这被认为是不好的做法,您可以改用
call_user_func() -
@zzzzBov 链接的问题中没有涉及,或者我只是无法在那里找到它?
-
@kingkero,我没有在我链接的问题中看到它,但可能有其他问题表明这是重复的
标签: php syntax brackets curly-braces