<?php use App\Caves_demographic_info; use App\Caves_current_uses; use App\Caves_flora_outside; public function get_page4_contd_data($id) { $tables = [ 'Caves_demographic_info', 'Caves_current_uses', 'Caves_flora_outside', ]; for($index = 0; $index < count($tables); $index++) { ${$tables[$index]."_model"} = new $tables[$index]; } } ?>
它会产生错误'Class{Class\u name}notfound',是否可以在循环中这样做?
你的方法似乎是对的,你能试试吗(用名称空间)
$className = 'App\' . $tables[$index]; $class = new $className;
在你的例子中是这样的:
$tables = [ 'App\Caves_demographic_info', 'App\Caves_current_uses', 'App\Caves_flora_outside', ]; for($index = 0; $index < count($tables); $index++) { ${$tables[$index]."_model"} = new $tables[$index]; }