Global function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Global function

Post by shiznatix »

ok i am sure this is a stupid question but how do I declare a function in the global scope of things? like if i have this:

Code: Select all

function dump($arr)
{
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
}
then i include that file into this file:

Code: Select all

require_once 'globalfunctions.php';

class thingy
{
    function thingy()
    {
        //i want to be able to use the dump function here
        dump(array(1, 2, 3, 4, 5));
    }
}
how do I do that?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Errr....have you actually tried it becuase the example you gave actually works

Code: Select all

function dump($arr)
{
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
} 

class thingy
{
    function thingy()
    {
        //i want to be able to use the dump function here
        dump(array(1, 2, 3, 4, 5));
    }
} 

$test = new thingy();
Outputs

Code: Select all

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ohhhhhhhhh. wow ok sorry obviously i didn't try it because i was certain that there was something special i had to do. sorry about that, it's definatly too hot out today.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

hehehe, no probs ;)
Post Reply