tags:

views:

68

answers:

2

In one PHP file, I have this code:

require_once $_SERVER['DOCUMENT_ROOT'] . '/custom/functions.php';
global $testVar;
var_dump($testVar);

In the functions.php file, I have this at the beginning, followed by a few other functions:

function pr($s) {
    echo '<pre>', htmlspecialchars(print_r($s,true)), '</pre>';
}

$testVar = 'hello world';

When running the first file, the variable comes back as NULL. I added the global bit but it shouldn't be necessary. This is part of a Joomla module but I've never had problems including files before, it should just work like regular PHP. Why might this be happening?

+1  A: 

First, try to use Joomla's path constants such as JPATH_BASE instead of $_SERVER['DOCUMENT_ROOT']. Joomla has a lot of useful constants, check it's documentation.

I've read your answer, and reading php documentation I tried to find a reason to why you need to use global keyword twice.

First, Variable scope.

The scope of a variable is the context within which it is defined. For the most 
part all PHP variables only have a single scope.

(...)

However, within user-defined functions a local function scope is introduced. 
Any variable used inside a function is by default limited to the local 
function scope.

The variable isn't in a function scope, so that's why we thought the NULL was a strange behavior.

But then I read include and found something interesting:

(...)
Any variables available at that line in the calling file will be available 
within the called file, from that point forward. However, all **functions** 
and **classes** defined in the included file have the global scope.

I can't see any mention about the variables being global in this paragraph. So,it seens that, being cumbersome or not, your solution is the right thing to do when you want to use global variables like that.

In your situation, if doing this is cumbersome, I would create a simple class. If you have just helper functions in your file, create a class Util{} with a lot of methods and $testVar as an attribute.

GmonC
You get an fatal error if the file is not found... are errors output to response?
asgerhallas
You're right, it shouldn't even echo "NULL" if there's a problem with $_SERVER['DOCUMENT_ROOT'] since he's using require_once instead of an include statement. My Bad. But the JPATH_BASE is still a good advice.
GmonC
The file is included fine and the functions in the file work. asgerhallas is right, it would have given an error if the file wasn't there. Thanks for the tip about `JPATH_BASE` though.
DisgruntledGoat
Yeah the Util class sounds like a neat idea. Really the few bits I'm using are variables that ought to be in the database but since there's only a couple it's much simpler and cleaner to use a config-like file.
DisgruntledGoat
A: 

I have found a solution that seems to work: using the global keyword both when setting the variable initially, and just before I need to use it.

(However this is quite cumbersome, and I'm still not sure why it happens, so if anyone has a better solution, feel free to post.)

DisgruntledGoat
I think I have an explanation to this behavior. It's not so small so I edited my own answer to explain. What do you think?
GmonC