variably named functions...

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
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

variably named functions...

Post by Dave2000 »

how might i get something similar to this to work...

Code: Select all

$variable = 'attack';

$new_gold = $old_gold + cost_$variable_gold(2);
I want to call a function depending on the value of $variable

How can i get this to work. It is just giving me the error...
Parse error: syntax error, unexpected T_VARIABLE in
Thank you

Shears :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Not certain about this, but it can't hurt to try it.

Code: Select all

<?php
$variable = 'attack';
$function = cost' . $variable . 'gold';

$new_gold = $old_gold + $function(2);
?>
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

Everah wrote:Not certain about this, but it can't hurt to try it.

Code: Select all

<?php
$variable = 'attack';
$function = cost' . $variable . 'gold';

$new_gold = $old_gold + $function(2);
?>

Code: Select all

<?php
$variable = 'attack';
$function = 'cost_' . $variable . '_gold';

$new_gold = $old_gold + $function(2);
?>
wyrmmage
Forum Commoner
Posts: 56
Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID

Post by wyrmmage »

yep, that's pretty much how you do it...you could always google 'variable composing' to see examples of this using variables also.
-wyrmmage
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

Thank you. Problem solved :)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

You'd need to eval() it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

bokehman wrote:You'd need to eval() it.
Most often no, you don't. Unless you're talking about something else or I missed something in the thread.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

feyd wrote:Unless you're talking about something else or I missed something in the thread.
It's quite possible I don't understand the question. I'm looking at this:

Code: Select all

$new_gold = $old_gold.'cost_'.$variable_gold.'(2);';
To get that to fire it would need to be evaled, wouldn't it? Anyway that's what I meant.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

bokehman wrote:

Code: Select all

$new_gold = $old_gold.'cost_'.$variable_gold.'(2);';
To get that to fire it would need to be evaled, wouldn't it? Anyway that's what I meant.
The way it is written in your snippet, yes it would need eval() however it could easily be adjusted to not need it at all.

Code: Select all

$func = $old_gold.'cost_'.$variable_gold; $new_gold = $func(2);
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Code: Select all

<?php

function foo($in)
{
	return $in*$in;
}

$func = 'foo';

echo $func(2);

?>
Thanks for clarifying that; I didn't know that construction existed.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Me either, until a couple of weeks ago. I remember asking the poster 'What are you trying to do with that bit of code?' Volka then showed me the light.
wyrmmage
Forum Commoner
Posts: 56
Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID

Post by wyrmmage »

wow...that is quite interesting code; thanks for posting it :)
Post Reply