【问题标题】:Method inside output buffer not working输出缓冲区内的方法不起作用
【发布时间】:2015-10-12 11:02:58
【问题描述】:

假设只有一个微不足道的 sn-p:

public function myMethod($file)
{
    require $file;
}

public function capture($file, array $args)
{
    extract($args, EXTR_SKIP);

    ob_start();

    $this->myMethod($file); //not working
    require $file; //works

    return ob_get_clean();
}

谁能解释一下为什么上面的 sn-p 只在使用require 的情况下有效,而在使用该方法时无效?

【问题讨论】:

  • 我认为你必须return你的require

标签: php oop require output-buffering


【解决方案1】:

您需要将参数提供给myMethod:

public function myMethod($file, array $args)
{
    extract($args, EXTR_SKIP);

    require $file;
}

public function capture($file, array $args)
{
    ob_start();

    $this->myMethod($file, $args);

    return ob_get_clean();
}

查看the scope of a variable

【讨论】:

    【解决方案2】:

    你不能在函数中包含(require)一个文件,因为它可以被多次调用,所以 require 将被再次调用 n .. 所以试试看

     public function myMethod($file)
        {
            require_once $file;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多