Page 1 of 1

Global function

Posted: Tue Jul 11, 2006 6:01 am
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?

Posted: Tue Jul 11, 2006 6:20 am
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.

Posted: Tue Jul 11, 2006 6:25 am
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.

Posted: Tue Jul 11, 2006 6:26 am
by JayBird
hehehe, no probs ;)