douna1892 2016-04-12 09:06
浏览 79
已采纳

像{variable}(variable | array)语句这样的括号在php中意味着什么?

I couldn't google this one. The question;

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);
}

Consider $endpoint is a variable and $args is an array of a class. We want to pass the variable $this->{$this->endpoint}($this->args) to _response() method. What does {$this->endpoint}($this->args) means in php syntax?

The link of full definition of code: http://coreymaynard.com/blog/creating-a-restful-api-with-php/

  • 写回答

1条回答 默认 最新

  • douzhaobo6488 2016-04-12 09:36
    关注
    $this->_response($this->{$this->endpoint}($this->args));
    

    Divide and conquer:

    $this->_response()
    

    Means calling the method _response() of the current object with the argument

    $this->{$this->endpoint}($this->args)
    

    The curly braces are explained here: http://php.net/manual/en/language.types.string.php

    Any scalar variable, array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\$ to get a literal {$.

    Therefore {$this->endpoint} evaluates to a string which was previously set as endpoint property of the current object.

    $this->endpointproperty($this->args)
    

    There must be a method endpoint property in the current object which accepts one argument. This Argument is also a property of this object:

    $this->args
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?