Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数返回php中的另一个函数_Php_Class_Oop - Fatal编程技术网

函数返回php中的另一个函数

函数返回php中的另一个函数,php,class,oop,Php,Class,Oop,我不知道这个问题(我问的方式)是否正确。我愿意接受你的建议。我想知道下面的代码是如何工作的。如果你需要任何细节,我可以提供我想要的 public function processAPI() { if (method_exists($this, $this->endpoint)) { return $this->_response($this->{$this->endpoint}($this->args)); } return

我不知道这个问题(我问的方式)是否正确。我愿意接受你的建议。我想知道下面的代码是如何工作的。如果你需要任何细节,我可以提供我想要的

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->端点是“myMethod”(我要执行的方法)

我在url中传递要执行的方法。函数捕获请求进程,然后调用确切的方法。我想知道它是如何工作的。尤其是这条线

return $this->_response($this->{$this->endpoint}($this->args));
PHP支持和

当它到达processApi中的语句时

return $this->_response($this->{$this->endpoint}($this->args));
PHP将解析您的端点变量,我们将用示例中的
myMethod
替换它:

return $this->_response($this->myMethod($this->args));
如您所见,我们现在正在调用一个存在于您的类中的方法。如果将端点设置为不存在的内容,则会抛出错误

如果myMethod返回一个字符串,例如
我的名字是bob
,那么一旦
$this->myMethod($this->args)
执行,PHP将解析该值作为
$this->\u response()
的参数,结果是:

return $this->_response('my name is bob');
在该事件链之后,
processAPI()
方法将最终返回JSON编码的字符串,这就是
\u response
方法所做的

PHP同时支持和

当它到达processApi中的语句时

return $this->_response($this->{$this->endpoint}($this->args));
PHP将解析您的端点变量,我们将用示例中的
myMethod
替换它:

return $this->_response($this->myMethod($this->args));
如您所见,我们现在正在调用一个存在于您的类中的方法。如果将端点设置为不存在的内容,则会抛出错误

如果myMethod返回一个字符串,例如
我的名字是bob
,那么一旦
$this->myMethod($this->args)
执行,PHP将解析该值作为
$this->\u response()
的参数,结果是:

return $this->_response('my name is bob');

在该事件链之后,
processAPI()
方法将最终返回JSON编码的字符串,这就是
\u response
方法所做的

你通过的方法怎么样?我只能看到类的内部,而不能看到如何使用它。这是什么课?框架?行
$this->{$this->endpoint}($this->args)
$this->endpointvariable($this->args)
相同,在您的例子中:
$this->myMethod($this->arg)
支持。端点周围的花括号告诉PHP在将该值用作变量之前先解析该值,请参见@magnus I pass method by url。不是一个框架,我在网上找到了这个教程。@chappell可以详细解释一下。那部分我想知道更多。请。当然,但是你在哪里通过这个方法?我没有看到任何公共方法接受任何参数。在现实生活中,您如何实际使用这个类?如果你在一篇教程中发现了这一点,那篇教程不应该告诉你它的作用吗?!你通过的方法怎么样?我只能看到类的内部,而不能看到如何使用它。这是什么课?框架?行
$this->{$this->endpoint}($this->args)
$this->endpointvariable($this->args)
相同,在您的例子中:
$this->myMethod($this->arg)
支持。端点周围的花括号告诉PHP在将该值用作变量之前先解析该值,请参见@magnus I pass method by url。不是一个框架,我在网上找到了这个教程。@chappell可以详细解释一下。那部分我想知道更多。请。当然,但是你在哪里通过这个方法?我没有看到任何公共方法接受任何参数。在现实生活中,您如何实际使用这个类?如果你在一篇教程中发现了这一点,那篇教程不应该告诉你它的作用吗?!很难更好地解释它。:)那更好。谢谢@magnus和@chappell!很难更好地解释它。:)那更好。谢谢@magnus和@chappell!