Search code examples
phpoutput-bufferingmissing-features

How can I set an output buffer name in php?


I have been browsing the documentation for PHP's ob_get_status function and found the following:

Return Values:

If called without the full_status parameter or with full_status = FALSE a simple array with the following elements is returned:

Array (
    [level] => 2
    [type] => 0
    [status] => 0
    [name] => URL-Rewriter
    [del] => 1 
)

All seems pretty clear, however all of the internets seem to be unable to answer one question that arose - how can I set the name of an output buffer?

Is it even possible to do it? I coudln't find any clue in the documentation itself, or anywhere else. However the documentation mentions that

name = Name of active output handler or ' default output handler' if none is set

which pretty much implies it is possible to set it somehow.

Do you guys have any idea if this can be done? Any help would be greatly apprieciated.


Solution

  • By using ob_start you can turn on the output buffering in PHP.

    Note that the function has such signature:

    bool ob_start ([ callable $output_callback = NULL [, int $chunk_size = 0 [, int $flags = PHP_OUTPUT_HANDLER_STDFLAGS ]]] )

    You can pass named function as a valid callback.

    For example:

    <?php
    
    function test_handler($a) {
        return $a;
    }
    
    ob_start('test_handler');
    
    var_dump(ob_get_status());
    

    Will give you:

    array(7) {
      ["name"]=>
      string(12) "test_handler"
      ["type"]=>
      int(1)
      ["flags"]=>
      int(113)
      ["level"]=>
      int(0)
      ["chunk_size"]=>
      int(0)
      ["buffer_size"]=>
      int(16384)
      ["buffer_used"]=>
      int(0)
    }