How to pass a callback function with parameters for ob_start in PHP?
Asked Answered
L

1

5

I've been following this tutorial on a caching function. I run into a problem of passing the callback function cache_page() for ob_start. How can I pass cache_page() along with two paramters $mid and $path to ob_start, something along the lines of

  ob_start("cache_page($mid,$path)");

Of course the above won't work. Here's the example code:

$mid = $_GET['mid'];

$path = "cacheFile";

define('CACHE_TIME', 12);

function cache_file($p,$m)
{
    return "directory/{$p}/{$m}.html";
}

function cache_display($p,$m)
{
    $file = cache_file($p,$m);

    // check that cache file exists and is not too old
    if(!file_exists($file)) return;

    if(filemtime($file) < time() - CACHE_TIME * 3600) return;

    header('Content-Encoding: gzip');

    // if so, display cache file and stop processing
    echo gzuncompress(file_get_contents($file));

    exit;
}

  // write to cache file
function cache_page($content,$p,$m)
{
  if(false !== ($f = @fopen(cache_file($p,$m), 'w'))) {
      fwrite($f, gzcompress($content)); 
      fclose($f);
  }
  return $content;
}

cache_display($path,$mid);

ob_start("cache_page"); ///// here's the problem
Lenz answered 25/4, 2015 at 6:32 Comment(1)
Can you please clarify a bit what the cache_pagefunction is supposed to do? I see it requires three arguments but you are only passing two arguments in the ob_start call. Likewise, the callback for ob_start has to have the signature string handler ( string $buffer [, int $phase ] )Hypoacidity
H
7

The signature of the callback to ob_start has to be:

string handler ( string $buffer [, int $phase ] )

Your cache_page method has an incompatible signature:

cache_page($content, $p, $m)

This means you are expecting different arguments ($p and $m) than ob_start will pass to the callback. There is no way to make ob_start change this behavior. It will not send $p and $m.

In the linked tutorial, the cache file name is derived from the request, e.g.

function cache_file()
{
    return CACHE_PATH . md5($_SERVER['REQUEST_URI']);
}

From your code I take you want to define the file path manually. What you can do then is this:

$p = 'cache';
$m = 'foo';

ob_start(function($buffer) use ($p, $m) {
    return cache_page($buffer, $p, $m);
});

This passes a compatible callback to ob_start which will call your cache_page function with the output buffer and closes over $p and $m into the callback.

Hypoacidity answered 25/4, 2015 at 9:17 Comment(1)
We should be able to up vote explanation and clarityWin

© 2022 - 2024 — McMap. All rights reserved.