php-如何以及为什么使用花括号:return $this-> {$this-> action}();

因此,在第一部分中,我制作了2个对象,实例化了两个类(我们从createController函数生成一个类)

$loader = new Loader(); 
$controller = $loader->createController(); 
$controller->executeAction();

方法executeAction代码

public function executeAction() {
    return $this->{$this->action}();
}

我的问题是采用以下代码行:$this-> {$this-> action}()此方法调用方式以及为什么使用花括号;试图执行action();扩展类的功能呢?

解决方法:

$this->{$this->action}();

表示应调用方法来自属性$this-> action.

$this->action = 'func1';
$this->{$this->action}();

等效于:

$this->func1();

有关更多示例,请参见PHP文档variable variablesvariable functions.需要大括号是因为$this-> $this-> action()通常被视为($this-> $this)-> action().

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...