douna1892 2016-04-12 17: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 17: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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制