January 28, 201115 yr Ok so the basics of what I have is a function call stored in a variable e.g. <?php $bar = 'doSomething(4)'; ?> and the function call stored in the variable I need it to call the function in the same script, so I have been trying: <?php eval('aClass::'.$bar); class aClass { function doSomething($foo) { echo $foo * $foo; } } ?> The php script doesn't execute because of the eval() function, am I doing something wrong here? Is there another way to go about doing this? Thank-you Jon
January 28, 201115 yr Could it be a lack of end of line terminator (semi-colon) inside the eval? E.g.: $bar = 'doSomething(4);'; Just a thought. Or you could equivalently do: eval("aClass::{$bar};"); Also, you might want to declare the class method as "public static function doSomething($foo)" otherwise it's an instance method and can only be invoked on an object, not on the class itself. Edited January 28, 201115 yr by Connetu_C
January 28, 201115 yr eval is evil! you dont need to use eval for what your trying todo... and in fact if your only left with eval as the only option theres something wrong with your script! also since your using static calling methods your function needs tobe declared as static... i have not tested it but the below should do somthing <?php $bar = 'doSomething(4)'; aClass::{$bar}; class aClass { function static doSomething($foo) { echo $foo * $foo; } } ?> Edited January 28, 201115 yr by SniderDK
January 28, 201115 yr Author It was the end line terminator in the eval() method. All works now. Thanks for the reply's! and I agree eval() method is evil, I tried putting the variable in parentheses like you showed but It didn't work. Theres nothing wrong with the script as its as simple as anything at the moment. but thanks for the reply!
January 28, 201115 yr well... i do call static methods using safe strings in places in my code... if you actually want to know how todo it without eval i can dig about and find it... i haven't touched that code in a fair few months so its not as fresh in my mind as it once was
January 28, 201115 yr Author That would be really useful. See what I'm trying to do is using some Ajax. The js on the front end is utilising Ajax to call PHP functions, so I thought the best way for me to do this is pass the function to be called through the Ajax via get and then the php gets this and calls the function. Hope that made sense. Is this the best way to go about doing it? Thanks again Mate!
January 29, 201115 yr <?php //example post var stuff $_POST['json'] = '{"class":"aClass","method":"doSomething","params":[9]}'; $ajax = json_decode($_POST['json']); class aClass { function doSomething($foo) { echo $foo * $foo; } } call_user_func_array(array(new $ajax->class, $ajax->method), $ajax->params); That code is not for a production environment! You will really want to limit what functions the user can call!
January 29, 201115 yr Author Thanks for the reply, Very interesting! I've never used any JSON before but am going to give it a try. I've had that worry in my head knowing that someone malicious could potentially use their own js to call a function I didn't intend them to call. Would you be able to explain how using your code would be harder to manipulate that just standard js string? Sorry if anything sounds a bit noobish here! Thanks
January 29, 201115 yr Well if a class/method/function does not exist then call_user_func_array will bail out with a warning. However if you're just evaluating a string as code, you're leaving your website wide open to abuse. e.g class aClass { public static function doSomething($foo) { echo $foo * $foo; } } $bar = 'doSomething(4); $output = shell_exec(\'ls -al\'); echo "<pre>$output</pre>";'; eval('aClass::'.$bar);
Create an account or sign in to comment