Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 533
RedGiant Avatar asked Apr 25 '15 06:04

RedGiant


People also ask

What can be passed to a callback parameter in PHP?

Apart from common user-defined function, anonymous functions and arrow functions can also be passed to a callback parameter. As of PHP 8.1.0, anonymous functions can also be created using the first class callable syntax . Generally, any object implementing __invoke () can also be passed to a callback parameter. echo 'hello world!';

What are the optional parameters of OB_start () function in PHP?

The ob_start () function of PHP Programming Language accepts many bunch of some optional parameters. They are: Flags. 1. Callback Function: The callback function parameter help in expecting a function that usually takes the output buffer’s content and then returns a string that is to be sent specifically to the browser for the rendering.

Why does this PHP callback return false?

If the callback is not a callable function, this function will return false . This is the callback signature: Contents of the output buffer. Bitmask of PHP_OUTPUT_HANDLER_* constants .

How to use callback functions with anonymous functions in PHP?

Use an anonymous function as a callback for PHP's array_map () function: User-defined functions and methods can also take callback functions as arguments. To use callback functions inside a user-defined function or method, call it by adding parentheses to the variable and pass arguments as with normal functions:


1 Answers

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.

like image 72
Gordon Avatar answered Oct 29 '22 15:10

Gordon