How and why use curly braces: return $this->{$this->action}();

前端 未结 2 1234
悲&欢浪女
悲&欢浪女 2021-01-23 01:29

so in first part i made 2 object, instantiate two classes (one we generate from createController function)

$loader = new Loader(); 
$controller = $loader->cre         


        
2条回答
  •  清酒与你
    2021-01-23 01:47

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

    means that the method that should be called comes from the property $this->action.

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

    is equivalent to:

    $this->func1();
    

    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().

提交回复
热议问题