【问题标题】:Global variable not working in php when using new ReflectMethod使用新 ReflectMethod 时全局变量在 php 中不起作用
【发布时间】:2013-09-19 12:13:52
【问题描述】:

我在使用全局变量时遇到了一些错误。 我在全局范围内定义了一个 $var 并尝试在函数中使用它,但在那里无法访问。请参阅下面的代码以获得更好的解释:

文件a.php:

<?php

  $tmp = "testing";

  function testFunction(){
     global $tmp;
     echo($tmp);
  }

关于如何调用这个函数。

文件b.php:

<?php
  include 'a.php'
  class testClass{
    public function testFunctionCall(){
        testFunction();
    }
  }

上面的 'b.php' 是通过以下方式调用的:

$method = new ReflectionMethod($this, $method);
$method->invoke();

现在所需的输出是“测试”,但收到的输出是 NULL。

提前感谢您的帮助。

我感觉这与 ReflectionMethod 的使用有关,但我不明白为什么它不可用。

【问题讨论】:

    标签: php php-5.4


    【解决方案1】:

    这对我来说很好。 [测试]。另外,是什么阻止您使用引用调用?

    <?php
    $tmp = "testing";
    
    function testFunction(){
    global $tmp;
    echo($tmp);
    }
    
    class testClass
    {
          public function testFunctionCall(){
              testFunction();
          }
    }
    
    $method = new ReflectionMethod($this, $method);
    $method->testFunctionCall();
    

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2013-07-13
      相关资源
      最近更新 更多