View Full Version : Variable function call

07-08-2005, 11:10 AM
I'm not exactly sure that the title is correct (if not let me know of a better one and I'll get it changed).

I want to be able to do the following (simplest form):

$sString = 'testFn';
if ($sString != null) $sString();

function testFn() {

The basis for wanting this is that I want to have a db full of tags and corresponding functions, so that I can loop through each of the tags and then run the corresponding function, and whatever value these functions return, replace the tags with.

I've been told that this may be possible but is probably bad practice. So what I also need is a method that has a similar outcome and is still deemed good coding. I'm quite willing to store the tags and function names in text files if that's another solution.

Many thanks in advance,

07-08-2005, 03:14 PM
PHP does support variable functions and, as far as I remember (having not actually used them) they work pretty much exactly as per your example. I'm not sure why this would be considered bad coding practice, so far as you check your input to make sure it can be trusted and you are careful about what output it allows you should be fine.

You can read more about variable functions on the php site (http://php.mirrors.ilisys.com.au/manual/en/functions.variable-functions.php) .

07-08-2005, 03:17 PM
but you could pass that $var to the function that does wateva..


07-09-2005, 01:25 AM
Thanks alot guys!

I don't see what would be bad practice about it either; I was planning on testing the values so there weren't null and I was also going to check that there was a function called $sString.

If anyone has a reason why this is deemed bad coding then let me know, but for now I'll go with it :)

Thanks again,

07-09-2005, 01:42 AM
I guess it could be a security issue if you allow users to create their own variables, in that case you might want to perhaps restrict which functions they can create variables for, but otherwise you should be good :thumbsup:

07-09-2005, 05:32 PM
Hmmm good point. I'm actually planning to use this method to allow ppl to create plugins and install them. Then they can edit the template files and put the appropriate tags in, and then have the tags replaced with functions defined by them. Maybe I need to rethink this area, what you think?

07-09-2005, 05:56 PM
I guess it comes down to trust as to how much power you give them, but it wouldn't be too hard to control. Just create an array of allowed function names and if the users try to call a function you don't want them to access and which isn't in your array then just output a message that usage of that function is restricted. You could even put all this stuff in the database too, then create different levels of user access rights, so most users just get access to a basic function set, some get access to restricted functions, etc.

I still think it could work, you just need to be very careful in how you implement it. If you're only giving them access to specific functions you've written yourself then this is very easy to control, just add all the function names to the array and check that the tag they create is in that array (using in_array() maybe) :thumbsup: