Executing a function when it's a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sphenn
Forum Commoner
Posts: 48
Joined: Sun Jul 17, 2005 8:08 pm
Location: Winnipeg, MB

Executing a function when it's a string

Post by Sphenn »

Hi,

I'm currently using a system that creates gathers a function name and required function arguments to be executed on the system. However, it outputs the resulting code as a string. For example, I can have something like:

Code: Select all

echo "$this->method($this->arguments)"; // Outputs some_func('foo', TRUE)
My problem is that I want to actually call some_func (which is a function in an included file.

I've tried using commands such as exec, shell_exec, and others, but they're not quite what I'm looking for. Also, simply writing:

Code: Select all

$this->method($this->arguments);
does not work.

If anyone has any comments, they would be appreciated.

Thank you,

Sphenn
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

eval()

Careful about the security implications though.
Sphenn
Forum Commoner
Posts: 48
Joined: Sun Jul 17, 2005 8:08 pm
Location: Winnipeg, MB

Post by Sphenn »

Ahh.... that's the function I was looking for. Maybe that's why exec sprang to mind.

Thanks so much

Sphenn
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

Try it.

Code: Select all

function my_callback_function() {
   echo 'hello world!';
}
call_user_func('my_callback_function');
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

$func = $this->method;
$func($this->arguments);
(#10850)
Post Reply