我不知道问题(我问的方式)是否正确。我接受你的建议。我想知道下面的代码是如何工作的。如果你想要我可以提供的任何细节,我想要的。一个函数返回另一个函数php
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);
}
private function _response($data, $status = 200) {
header("HTTP/1.1 " . $status . " " . $this->_requestStatus($status));
return json_encode($data);
}
private function _requestStatus($code) {
$status = array(
200 => 'OK',
404 => 'Not Found',
405 => 'Method Not Allowed',
500 => 'Internal Server Error',
);
return ($status[$code])?$status[$code]:$status[500];
}
/**
* Example of an Endpoint
*/
protected function myMethod() {
if ($this->method == 'GET') {
return "Your name is " . $this->User->name;
} else {
return "Only accepts GET requests";
}
}
这里$this->endpoint is 'myMethod' (a method I want to execute)
我通过,我想在URL中执行的方法。该函数捕获请求过程,然后调用确切的方法。我想知道它是如何工作的。特别是这条线。
return $this->_response($this->{$this->endpoint}($this->args));
你通过该方法如何?我只能看到班级的内部,而不是你如何使用它。这是什么课程?框架? $ this-> {$ this-> endpoint}($ this-> args)'与$ this-> theValueOfTheEndpointVariable($ this-> args)'相同,在你的情况下:'$ this-> myMethod的($这个 - > ARG)'。 –
PHP支持[变量函数](http://php.net/manual/en/functions.variable-functions.php)。你的端点周围的花括号告诉PHP在使用它作为变量之前解析该值。参见[PHP变量变量](http://php.net/manual/en/language.variables.variable.php) –
@magnus I pass方法由url。不是我在互联网上找到这个教程的框架。 –