How do I make functions in an array?

Discussion in 'PHP' started by wwwbryan, Jul 26, 2009.

  1. #1
    For example, I have a list like this in php:

    
    <?php
    $GLOBAL->getTopbar();
    $GLOBAL->getBottombar();
    $GLOBAL->getMonkeybar();
    $GLOBAL->getBigbar();
    ?>
    
    PHP:
    Which each thing calls a class to get parts of my website layout.

    But to make it more organized I want to have it like something like this:

    
    <?php
    
    $array = array($GLOBAL->getTopbar(), $GLOBAL->getBottombar(), $GLOBAL->getMonkeybar(), $GLOBAL->getBigbar());
    
    echo $array;
    
    ?>
    
    PHP:
    Something like that, but how would I go about doing that? Well in a more simpler way.
     
    wwwbryan, Jul 26, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Run a

    print_r($array)

    to see what the array contains.

    You could also do:
    $array = array(
    'topBar'=>$GLOBAL->getTopbar(),
    'bottomBar'=>$GLOBAL->getBottombar(),
    'monkeyBar'=>$GLOBAL->getMonkeybar(),
    'bigBar'=>$GLOBAL->getBigbar());

    Access via: $array['topBar']; etc...

    http://www.php.net/array
     
    jestep, Jul 26, 2009 IP
  3. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    First off, I've never had to use $GLOBAL, or global for that matter except within a function where the local scope takes over.

    Secondly if you're refering to commands like.
    
    $Site->FunctionName();
    
    PHP:
    That's not an array but a class. For example your stuff above.
    
    class MyClass
    {
        function getTopBar()
        {
    ...
        }
    
        function getBotomBar()
        {
    ...
        }
    }
    
    
    $mySite = new MyClass;
    
    $mySite->getTopBar();
    
    PHP:
    Course I've always used include() to bring in pieces like top, middle, sidebar, etc.

    If $Global is from a class... why you need to put it's functions into an array, wouldn't that just make the code longer?
     
    kblessinggr, Jul 26, 2009 IP
  4. wwwbryan

    wwwbryan Well-Known Member

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #4
    Ok jestep, I was off by a little bit and thanks. Also how do I echo or print everything in the array without having:
    Array (blah) at the bottom of the layout?
     
    Last edited: Jul 26, 2009
    wwwbryan, Jul 26, 2009 IP
  5. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try a

     echo '<pre>'.print_r($array, true).'</pre>';
    PHP:
     
    Steve136, Jul 26, 2009 IP