Class MyClass{
private $data=array('action'=>'insert');
public function insert(){
echo 'called insert';
}
public function run(){
$this->$this->data['action']();
}
}
これは機能しません:
$this->$this->data['action']();
使用する唯一の可能性ですかcall_user_func();
?
ベストアンサー1
試す:
$this->{$this->data['action']}();
アクションが許可されていて、呼び出し可能であるかどうかを必ず確認してください。
<?php
$action = 'myAction';
// Always use an allow-list approach to check for validity
$allowedActions = [
'myAction',
'otherAction',
];
if (!in_array($action, $allowedActions, true)) {
throw new Exception('Action is not allowed');
}
if (!is_callable([$this, $action])) {
// Throw an exception or call some other action, e.g. $this->default()
throw new Exception('Action is not callable');
}
// At this point we know it's an allowed action, and it is callable
$this->$action();