Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post

    Variable function call

    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):

    Code:
    $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,
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 .

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    but you could pass that $var to the function that does wateva..

    testFn($var);
    --
    tom :)

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post
    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,
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post
    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?
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •