JREAM
October 7, 2010, 1:36pm
1
Sup guys, I can’t remember how I did this before, there is a command I know to do something.
So if I have these two functions:
imageCreateFromPNG()
imageCreateFromGIF()
I want to use a variable for the last part so…
function create($var)
{
imageCreateFrom{$var}();
}
I think if that doesnt work you can do like…
$string = “imageCreateFrom.$var”;
and Use the string as a function: $string()??
PS offtopic: The font thats spose to be monospace on Chrome shows up weird on ubuntu
JREAM
October 7, 2010, 3:20pm
2
Awesome thanks guys!!
Call user func was what i was looking for but i like the variable way.
For your help, here is some morning/afternoon motivation:
Eye of the Tiger
[fphp]call_user_func/fphp would be preferred I think. However each of the imagecreatefrom* functions require parameters to be passed, so [fphp]call_user_func_array/fphp would be better suited.
<?php
function create_image($type, $args = array()){
$function = 'imagecreatefrom' . $type;
if(function_exists($function)){
return call_user_func_array($function, $args);
}
return false;
}
$image = create_image('jpeg', array('filename.jpg'));
?>
If you’re going to be passing the filename in anyway, you may as well base your decision on which function to use on that.
What Anthony suggests is a very elegant solution and you should consider that. You can also use your first suggestion, ie a function name as a variable
$createFunction = "imageCreateFrom" . $type;
$createFunction(...);
The function names are all lowercase though, so you should probably watch your casing to avoid issues on linux