php函数全局定义的变量不适用于Yii

I am having problems with some legacy code that I am trying to add to a Yii project.

It has to do with global variables, which I am well aware should instead be passed as parameters, but since this old code and is used in other projects rewriting it is not really and option.

$testVar = '123';
function testOutput() {
   global $testVar;
   var_dump($testVar);
}
testOutput();

Now if I include this file in a plain php file it works and outputs

string '123' (length=3)

But if I include this file in a Yii controller or even in a template it output this

null

I have tried to search for this issue but I just get a bunch of results about people using global variables incorrectly. I am sure it is not actually a Yii issue but most likely a php_ini setting that Yii is setting, but I can't find anything when searching the code or the Yii docs that would explain this.

This example can be tested by just creating a file with my first code block and then include it into a Yii template or controller. I even tested it with a clean example Yii project.

I hope I didn't hurt my chances of figuring this out by tagging this questiong with Yii since I have a feeling that it is not just a Yii specific issue.

Any insights would be greatly appreciated.

If you do like this, it will work, I just tested with Yii controller

global $testVar;
$testVar = '123';
function testOutput() {
   global $testVar;
   var_dump($testVar);
}
testOutput();

As DCoder mentioned, if youre declaring them inside a class, function/method then they are not global. You can try assigning them to the $_GLOBALS array though:

$GLOBALS['testVar'] = 123;

However depending on the legacy code and how youre integrating it you may need to change all references in that legacy code to use $GLOBALS['thevar'] instead of $thevar or do an extract($GLOBALS) at the top of some or all of the legacy files.