SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    link a function of a function

    I still have troubles with this here

    function first() {

    function second() {
    // ..
    }
    }

    if ($_GET['action'] == 'second') first(second());

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you put a function within a function, you can only fire said function from inside the parent function.

    So you have two choices:
    PHP Code:
    <?php
    function first($val){
       
    ///
    }
    function 
    second(){
       
    ///
       
    return 'something';
    }
    first(second());
    ?>
    or
    PHP Code:
    <?php
    function first($command){
        switch(
    $command){
            case 
    'second':
                
    //execute code here
            
    break;
        }
    }
    first('second');
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    first();second();

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It depends what you want to accomplish really.

    P.s. thanks for the siglink
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, if you define a function inside a function, you can call the inner function, but you must call the outer function to define the inner function. But if you call the outer function twice, then the inner function will be defined twice, and PHP will throw a fatal error.

    Function declarations are still very procedural; however, when in the global/top scope, you can put function declarations in any order, even before you call them.

  6. #6
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess what Im trying to ask is this:
    I have a function inside a function, I want to call it from this link:

    <a href='?action=first&second&page=".$page[$ids]."'

  7. #7
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Could be you don't really want nested functions. Do you just want to call function2 from within function1?

    If you really do want it that way, then what you posted above is true.
    first();second();

    But, as sk89q said, you can't call first() again in that script. (unless you modified it to check if second() was defined before defining it)
    - Robert

  8. #8
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I decided to drop my doubled up function,
    and go with ?action=function1&do=function2 -- where do=function2 is just a get request inside of function 1

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a better way to do it.

    However, if you really wanted to do it your original way (not recommended)...
    PHP Code:
    <?php
    $action 
    explode(","$_GET['action'], 2);

    // Outer function
    function member() {
        global 
    $member;
        
    $member $_GET['id'];
        
        
    // Inner function
        
    function burn() {
            global 
    $member// burn() has NO relation to member(), so we need to pollute the global namespace with $member
            
    echo 'Burning member $member...';
        }

        
    // Inner function
        
    function create() {
            global 
    $member// create() has NO relation to member(), so we need to pollute the global namespace with $member
            
    echo 'Creating member $member...';
        }
    }

    if (
    in_array($action[0], array('member''record''baby'/* Allowed outer functions */)) {
        if (!
    function_exists($action[1])) { // The inner function should not be defined yet
            
    $action[0](); // This will define the inner function
            
    if (function_exists($action[1])) { // Now it should exist
                
    $action[1](); // Call inner function
            
    } else {
                echo 
    "Invalid action (3)";
                exit;
            }
        } else {
            echo 
    "Invalid action (2)";
            exit;
        }
    } else {
        echo 
    "Invalid action (1)";
        exit;
    }
    http://example.com/test.php?action=member,burn


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
  •