tags:

views:

72

answers:

2

Invalid code:

$functionName = 'sayThis';

function $functionName($string) {
    echo $string;
}

Can I do anything like this?

+2  A: 

call_user_func and call_user_func_array do something similar. Use sparingly and judiciously.

Ewan Todd
I want to dynamically declare the function name
Kirk
+2  A: 

I don't recommend you do this, but if you insist, using eval is an option.
Be extremely careful tho. And keep it far far away from any user input!

<?php
$name = 'sayThis';

$code = <<<PHP
function $name(\$string) {
    echo \$string;
}
PHP;
eval($code);

$name('This is NOT a good idea!');
?>
Atli
+1 This looks like it answers the OP's question. It is one of those things to be careful what you ask for.
Ewan Todd