tags:

views:

156

answers:

5

Hi guys, I have a rather stupid PHP question :D! I would like to simplify the following statement:

function hello_input() {
    return 'Hello World';
}

$helper = 'hello';

$helper = $helper . '_input';

$data = $helperinput();

The specific part I want to simplify is the adding of the _input to the $helper so it calls the right function.

I thought of something like this but it doesn't work:

$data = $helper. 'input'();
or
$data = $helper. 'input' . ();

Any ideas?

Thanks, Max

+12  A: 

Use call_user_func, e.g. $data = call_user_func($helper.'input');.

PiotrLegnica
Awesome you rock!
mistero
A: 

Although I recommend against it, you could do something like this:

$statement = $helper . '_input();';
$data = eval($statement);

The reason I recommend against it is that you'll be opening yourself up for code-injection attacks. But if it solves a problem and you can sanitize your inputs, have fun with it.

Edit:

I like @PiotrLegnica's solution better than mine. Much safer!

I was unaware of it. I guess you learn something new every day.

Randolpho
Unsafe!!! *OH MY EYES NOOO*
Havenard
A: 

Get the whole function name into a string first.

$helper = 'hello';
$func = $helper . '_input';
$data = $func();

PHP calls this variable functions. See also variable variables.

Adam Backstrom
He's already doing that, and asks for another way.
PiotrLegnica
That's not true at all.
Adam Backstrom
It's what his first code sample does (although variable name is wrong, but I assume that's mistake left by `$helper. 'input'();`-like experiments). And he asks how to simplify it.
PiotrLegnica
I made no such assumption about his example. My answer is the corrected syntax of his two non-working attempts at the bottom of his question.
Adam Backstrom
Nice. Got somebody following you around, upvoting you and downvoting me?
Adam Backstrom
A: 
$helper = 'hello_';
$data = $helper.'input';
call_user_func($data);

That should get you most of the way there. Good luck!

Docs here

inkedmn
A: 

Are you expecting to invoke code based on an input? You should never try to do that. That gives people the chance to execute arbitrary code. Even if you can't see quite how, it's still a potential method of attack.

What you probably want is a switch statement (or chained if statements) that match an input and dispatch the correct function.

Joe
Not arbitrary code, just funtcions in a special namespace that are designed to be called from outside. Saying this is insecure, is like saying that it's insecure to let the visitor of your site choose which php file to execute.
JasonWoof
If they're in a special namespace, fine. But I would still say that this hints at a less-than-optimal design. Reflection (and its analogues) don't belong production code. In my opinion.
Joe
Hey thanks for pointing that out however this is for an API and you can only make calls with several different functions (I mean if the function doesn't exist there is nothing you can do). As in "plugins"...and in the POST only defined values are allowed...
mistero