tags:

views:

138

answers:

4

Currently, I'm passing a command into a user-defined function in PHP. How do I have PHP execute this when I ask it to?

+3  A: 

You'll want to either use eval or call_user_func, depending on whether it's a set of expressions or simply a function call.

karim79
+1  A: 

If it's a shell command you want to execute you use backticks or shell_exec.

lemonad
A: 

As eval() would be able to arbitrarily execute code on your server, maybe you can generate class files from your user application with any functions that a user might be authorized to call, then check for existence of a function using introspection in the newly generated class file first.

FilmJ
+1  A: 

If its a method in a class, maybe this will be useful :

// The method to call
$proposed_method = 'some_method_name';

// See if its available
if (is_callable(array($this,$proposed_method), FALSE))
{
    // Call the method
 return $this->$proposed_method();
}

Where $this in the is_callable refers to the current class.

ae