Call method by string?

后端 未结 2 1389
鱼传尺愫
鱼传尺愫 2020-12-05 04:23
Class MyClass{
  private $data=array(\'action\'=>\'insert\');
  public function insert(){
    echo \'called insert\';
  }

  public function run(){
    $this->         


        
2条回答
  •  我在风中等你
    2020-12-05 04:41

    Try:

    $this->{$this->data['action']}();
    

    You can do it safely by checking if it is callable first:

    $action = $this->data['action'];
    if(is_callable(array($this, $action))){
        $this->$action();
    }else{
        $this->default(); //or some kind of error message
    }
    

提交回复
热议问题