6

i'd like to run a php-function dynamically by using this string:

do_lightbox('image1.jpg', 'picture 1')

i've parsed the string like this:

$exe = "do_lightbox";
$pars = "'image1.jpg', 'picture 1'";

and tried using the following code:

$rc = call_user_func($exe, $pars);

unfortunately this gives me an error - i've also tried splitting the $pars like

$pars = explode(',', $pars);

but didn't help ..

any ideas? thanks

3
  • This issue is a symptom, that something wrong in architecture of your application. Commented Jun 4, 2011 at 13:38
  • Is there a particular reason as to why you would want to do this, it can cause security issues but it just means that you are writing more lines of code for the same effect... You could change the parameters passed to the function so that they were dynamically loaded (like you want). Commented Jun 4, 2011 at 13:40
  • it's for embedding text-blocks into my html code which will be executed. Commented Jun 4, 2011 at 14:17

6 Answers 6

7

I think this is what you're after:

$exe = "do_lightbox";
$pars = array('image1.jpg', 'picture 1');

$rc = call_user_func_array($exe, $pars);
Sign up to request clarification or add additional context in comments.

Comments

2

$pars must be an array with parameters in it. Should be : array('image1.jpg', 'picture 1') but with your method, it is : array("'image1.jpg'", " 'picture 1'") which isn't what you are looking for.

Comments

2

This is an example of how call_user_func() works:

function myfunc($p1,$p2){
    echo "first: $p1, second: $p2\n";
}

$a1="someval";
$a2="someotherval";
call_user_func("myfunc",$a1,$a2);

The difference here from previous examples it that you don't have to pass each argument in a single array. Also, you can parse an array of delimited strings and do the same thing:

function myfunc($p1,$p2){
    echo "first: $p1, second: $p2\n";
}

$a="someval, someotherval";
$e=explode(", ",$a);
$a1=$e[0];
$a2=$e[1];
call_user_func("myfunc",$a1,$a2);

Comments

1

Best to use call_user_func_array which allows you to pass arguments like an array:

call_user_func_array($exe, $pars);

Alternatively you can use eval to directly parse a string (but I do not recommend this):

eval("do_lightbox('image1.jpg', 'picture 1')");

Which will execute your function.

2 Comments

Sorry, yeah I mistyped exec and eval in the first go, my appologies. Still eval is not the way to go!
No, you did use exec in a first place. Exec launch an external program, the parameter being a command line, wich isnot what is intended here. Obviously, you edited your post, so this is not wrong anymore.
1

Although I wonder why do you need such functionality (security problems), here is a solution:

$exe = "do_lightbox";
$pars = "'image1.jpg', 'picture 1'";
call_user_func_array($exe, explode(',', $pars));

You may also want to get rid of the single quotes and spaces around image filenames.

Comments

1

All though it is strongly discouraged you can use the eval function:

eval("do_lightbox('image1.jpg', 'picture 1')")

2 Comments

Usually eval needs to be avoided, espacially when the language gives you tools like call_user_func_array or ReflectionMethod.
I agree. From the question however the input seemed to be a string with function and parameters. The parsing of the string for making input to call_user_func_array should be that hard, and I would go for that, just giving an alternative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.