你可以在同一个类中调用方法,那么动态吗?
在一个类中我想要一个带有模式的不同方法,所以你可以在foreach循环中调用它们
class MyClass
{
private $tables;
function __construct(){
$this->tables = array('a','b','n');
}
public function runMigrations() {
foreach ($this->tables as $table) {
// Excecute method migration_$table)
call_user_func('migration_'.$table.'()'); // Try but not work
// call_user_func('this->migration_'.$table.'()');; // Try but not work
// call_user_func('$this->migration_'.$table.'()');
}
}
private function migration_a() {
// Some code
}
..
private function migration_n() {
// Some code
}
}
[ErrorException]
call_user_func() expects parameter 1 to be a valid callback, function 'migration_a()' not found or invalid function name