SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,791
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)

    string variables and string functions.

    I am being somewhat theoretical here. In PHP, I can use a string to select a function to call by doing something like this... which calls the function foo().
    $fun="foo"
    $fun();

    My question is, since I can use that method to call different functions, what can i used to pass whatever variables I needed to each different function that might be called???


    Just as an example of what i am thinking...Let's say i had something like:

    function foo1($input1, $input2, $input3){
    // code here
    }

    function foo2($input1){
    // code here
    }

    function foo3($input1, $input2, $input3,$input4){
    // code here
    }
    function FooHub($fooToCall, $varsToPass){
    $ret=$fooToCall( $varsToPass);
    return $ret
    }


    for example:
    FooHub("foo1", "'the next value is an integer',10, TRUE");

    FooHub() WILL call the function foo1, but it will pass only one sting with the value of : "'the next value is an integer',10, TRUE", instead of three separate values as is needed.

    I suppose I could parse $varsToPass, converting it onto separate variables, or send the values separately to FooHub(), but then that would mean I would need to have different call statements depending on the variables each function that FooHub() is calling expects to receive, this however totally negates the advantage of this method.

    Am I trying to do something impossible, or is there a way to pass a "stringed argument" to a function?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,045
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Just a quick thought. Maybe you could put the variables into an array? Or maybe give the functions "extra" parameters with default values of NULL?

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    <?php
    function foo($param1,$param2,$param3) {
        echo 
    "<p>$param1 : $param2 : $param3</p>";
    }

    call_user_func_array('foo',array('one','two','three'));
    ?>
    Little more complex:

    PHP Code:
    function foo3($param1,$param2,$param3) {
        echo 
    "<p>Foo3:: $param1 : $param2 : $param3</p>";
    }

    function 
    foo2($param1,$param2,$param3) {
        echo 
    "<p>Foo2:: $param1 : $param2 : $param3</p>";
        
    $arrArgs func_get_args();
        
    call_user_func_array('foo3',$arrArgs);
    }

    function 
    foo($param1,$param2,$param3) {
        echo 
    "<p>Foo:: $param1 : $param2 : $param3</p>";
        
    $arrArgs func_get_args();
        
    call_user_func_array('foo2',$arrArgs);
    }

    call_user_func_array('foo',array('one','two','three')); 
    So the answer to your question is call_user_func_array().

  4. #4
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,791
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    Thanks, to both of you.
    it makes sense ... i could use my old methow with "place holder=NULL" variables, what's more I wasnt aware of "call_user_func_array" That just makes things TOO simple..lol ==


Bookmarks

Posting Permissions

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