简体   繁体   中英

Method inside output buffer not working

Assume having just a trivial snippet:

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();
}

Could anyone explain why the snippet above works in case of using require only and not when using the method?

You need to make available the parameters to 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();
}

See about the scope of a variable .

You can not include (require) a file in function since it can be call many times , so require will be called again n again .. So have a try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM