我无法用谷歌搜索这个。问题;
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()
方法。{$this->endpoint}($this->args)
在php语法中意味着什么?
代码完整定义的链接:http://coreymaynard.com/blog/creating-a-restful-api-with-php/
$this->_response($this->{$this->endpoint}($this->args));
分而治之:
$this->_response()
表示使用参数调用当前对象的方法_reresponse()
$this->{$this->endpoint}($this->args)
花括号的解释如下:http://php.net/manual/en/language.types.string.php
任何带字符串的标量变量、数组元素或对象属性可以通过该语法来包括表示。只需编写表达式的方式与它在字符串外显示的方式相同,并且然后用{和}包起来。由于{无法转义,因此此语法将只有当$紧跟在{后面时才能识别。使用{''$获取文字{$.
因此,{$this->endpoint}计算为一个字符串,该字符串以前被设置为当前对象的endpoint属性。
$this->endpointproperty($this->args)
当前对象中必须有一个接受一个参数的方法终结点属性。此参数也是此对象的属性:
$this->args