dongxun3777 2013-03-04 19:40
浏览 141
已采纳

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.

  • 写回答

3条回答 默认 最新

  • duanhan5388 2013-03-04 20:27
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)