php - How to use call_user_func() instead of eval()? -
i needed automatically parameters $_get['url'] variable. thought i'll make using eval() function.
as i've heard how not-secure is, began search solution , i've found function called call_user_func(), don't know how use it.
this code, worked correctly, don't want use anymore. can tell me how make working call_user_func()?
$execute = '$controller->' . "$url[1]("; for($i=2; $i<count($url); $i++){ $execute .= "'" . $url[$i] . "'"; if($i<count($url)-1){ $execute .= ", "; } } $execute .= ');'; eval($execute);
you might want use call_user_func_array instead:
if (method_exists($controller, $url[1])) { call_user_func_array(array($controller, $url[1]), array_splice($url, -2)); } basically check if method exists, if call list of arguments. doing:
$controller->{$url[1]}($url[2], $url[3], $url[4], ..); call_user_func ideal if have no parameters call, or if parameters want call in 1 array variable, function test (array $arguments).
Comments
Post a Comment