如何和为什么使用花括号:return $ this-> {$ this-> action}(); [英] How and why use curly braces: return $this->{$this->action}();
问题描述
所以在第一部分我做了2个对象,实例化两个类(一个我们从createController函数生成)
so in first part i made 2 object, instantiate two classes (one we generate from createController function)
$loader = new Loader();
$controller = $loader->createController();
$controller->executeAction();
和方法 executeAction
p>
and the method executeAction
code:
public function executeAction() {
return $this->{$this->action}();
}
我的问题是这行代码: $ this-> {$ this-> action}()
如何调用此方法以及为什么使用花括号;试图执行扩展类的 action();
函数可能?
my question is taking this line of code: $this->{$this->action}()
how this method is called and why use curly braces; Is trying to execute the action();
function of extended class maybe?
推荐答案
$this->{$this->action}();
表示应该调用的方法来自属性 $ this- > action
。
means that the method that should be called comes from the property $this->action
.
$this->action = 'func1';
$this->{$this->action}();
等效于:
$this->func1();
请参阅变量变量和变量函数,以了解更多示例。需要大括号,因为 $ this-> $ this-> action()
通常被视为($ this-> $ this ) - > action()
。
See the PHP documentation of variable variables and variable functions for more examples of this. The braces are needed because $this->$this->action()
would normally be treated as ($this->$this)->action()
.
这篇关于如何和为什么使用花括号:return $ this-> {$ this-> action}();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!