PHP: eval 没有价值
PHP: eval don't have value
我在数据库中存储了一个 Controller@Action
,但是当我尝试 运行 它时它没有价值。
我知道 eval 很危险。如果有替代方案我可以在这种情况下使用,我愿意接受。
我不想使用的代码 运行 它:
//$action = "ExchangeController::module_getAllValutaByBank";
//$params = "K&H";
$test = eval('\App\Http\Controllers\ModuleControllers' . "\" . $action . "('" . $params . "');");
Debugbar::info($test);
我要评估的代码:
It's an API request to the local ForexChange site. @param bank's name
@return stuctured string
public static function module_getAllValutaByBank($bankName){
$return = '';
$data['bankName'] = $bankName;
$response = self::getRequest($data);
if(is_array($response)){
$return .= $response[0]['bank'] . "\n";
foreach($response as $key => $item){
$return .= $item['valuta'] . " - " . round($item['buy'], 2) . " - " . round($item['sell'], 2) . "\n";
}
} else {
$return = $response;
}
Debugbar::info($return);
return $return;
}
Debugbar::info($return);
的输出
K&H Bank GBP - 338.51 - 363.07 AUD - 189.9 - 207.8 DKK - 39.75 - 43.93
JPY - 2.26 - 2.48 CAD - 188.73 - 206.51 NOK - 30.92 - 34.18 CHF -
256.89 - 275.53 SEK - 29.18 - 32.26 USD - 246.14 - 260.32 CZK - 11.51 - 12.97 PLN - 69.87 - 78.79 EUR - 302.92 - 320.38 HRK - 39.39 - 44.41
Debugbar::info($test);
的输出
null
哪里出错了?
编辑:
找到解决方案:
$test = call_user_func('\App\Http\Controllers\ModuleControllers' . "\" . $action, $params));
找到解决方案:
$test = call_user_func('\App\Http\Controllers\ModuleControllers' . "\" . $action, $params));
我在数据库中存储了一个 Controller@Action
,但是当我尝试 运行 它时它没有价值。
我知道 eval 很危险。如果有替代方案我可以在这种情况下使用,我愿意接受。
我不想使用的代码 运行 它:
//$action = "ExchangeController::module_getAllValutaByBank";
//$params = "K&H";
$test = eval('\App\Http\Controllers\ModuleControllers' . "\" . $action . "('" . $params . "');");
Debugbar::info($test);
我要评估的代码:
It's an API request to the local ForexChange site. @param bank's name @return stuctured string
public static function module_getAllValutaByBank($bankName){
$return = '';
$data['bankName'] = $bankName;
$response = self::getRequest($data);
if(is_array($response)){
$return .= $response[0]['bank'] . "\n";
foreach($response as $key => $item){
$return .= $item['valuta'] . " - " . round($item['buy'], 2) . " - " . round($item['sell'], 2) . "\n";
}
} else {
$return = $response;
}
Debugbar::info($return);
return $return;
}
Debugbar::info($return);
K&H Bank GBP - 338.51 - 363.07 AUD - 189.9 - 207.8 DKK - 39.75 - 43.93 JPY - 2.26 - 2.48 CAD - 188.73 - 206.51 NOK - 30.92 - 34.18 CHF - 256.89 - 275.53 SEK - 29.18 - 32.26 USD - 246.14 - 260.32 CZK - 11.51 - 12.97 PLN - 69.87 - 78.79 EUR - 302.92 - 320.38 HRK - 39.39 - 44.41
Debugbar::info($test);
null
哪里出错了?
编辑:
找到解决方案:
$test = call_user_func('\App\Http\Controllers\ModuleControllers' . "\" . $action, $params));
找到解决方案:
$test = call_user_func('\App\Http\Controllers\ModuleControllers' . "\" . $action, $params));