+ Reply to Thread
Results 1 to 3 of 3

Thread: Php coding help

  1. #1
    taekwondokid42 is offline x10 Lieutenant taekwondokid42 is an unknown quantity at this point
    Join Date
    Aug 2007
    Posts
    268

    Php coding help

    I am wondering if there is a way to modify a function with code, for example a forloop or something.

    Example:

    function dostuff() {
    otherfunction1;
    otherfunction2;
    otherfunction5;
    }

    based on user input (which could be infinitely varied), I want to be able to change dostuff(), so that in one case it would call of5 first and then of2 and not call of1 at all.

    Am I clear and is this possible?

  2. #2
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Php coding help

    Check out Variable Functions

    It may be what you are looking for.

    Edit:

    Here you have an example:

    PHP Code:
    <?php
    function test1(){
        echo 
    "This is function 1" "<br/>";
    }

    function 
    test2(){
        echo 
    "This is function 2" "<br/>";
    }

    function 
    test3(){
        echo 
    "This is function 3" "<br/>";
    }

    function 
    dostuff($stuff){
      
    reset($stuff); //Make sure it starts at the beginning
      
    while( key($stuff) !== NULL )
      {
        
    $do current($stuff);
        
    $do(); // Here is where each function is called.
      
    next($stuff);
      }
    }


    // Change $stuff to the functions you want to call
    $stuff = array('test1','test3');

    //Test the variable function
    dostuff($stuff);
    /*
    Output will be:
    This is function 1
    This is function 3
    */

    ?>
    Last edited by gomarc; 04-02-2010 at 08:40 PM.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Php coding help

    In addition to variable functions, there are call_user_func, call_user_func_array and even ReflectionMethod. Just be careful not to set the function name directly from user input, else you'll be opening your script to injection.

    PHP Code:
    $allowedThings array_flip(array('aFunc''anotherFunc''yaf', ...));

    function 
    dostuff($stuff) {
        foreach (
    $stuff as $thing => $args) {
            if (
    is_array($args)) {
                
    call_user_func_array($thing$args);
            } else {
                
    call_user_func($thing$args);
            }
        }
    }

    $stuffToDo array_intersect($stuff$_REQUEST[]);
    dostuff($stuffToDo); 
    Last edited by misson; 04-03-2010 at 02:25 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Coding Help
    By kesne in forum Ads & Offers
    Replies: 2
    Last Post: 02-12-2009, 09:23 PM
  2. Coding
    By XGI Admin in forum Programming Help
    Replies: 2
    Last Post: 02-06-2009, 08:25 PM
  3. Coding help
    By MicrotechXP in forum Programming Help
    Replies: 18
    Last Post: 02-29-2008, 08:01 PM
  4. Coding help
    By MicrotechXP in forum Programming Help
    Replies: 5
    Last Post: 02-10-2008, 02:56 PM
  5. Help with coding
    By trevor51590 in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 03-23-2006, 03:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers