4

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.

Kalko
  • 406
  • 3
  • 11
  • You can't really set a name. `name` will be equal to the function name which handles the output buffering, e.g. `mb_output_handler`. http://php.net/mb_output_handler – Charlotte Dunois Jun 20 '16 at 13:07

3 Answers3

2

You can't set the name. It prints the name of the callback.

If your callback is a function called foo_bar:

[name] => "foo_bar"

If your callback is the method foo_bar of a class Acme:

[name] => "Acme::foo_bar"

If your callback is an anonymous function:

[name] => "Closure::__invoke"
Shira
  • 6,392
  • 2
  • 25
  • 27
2

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)
}
Gino Pane
  • 4,740
  • 5
  • 31
  • 46
1

The name that you set on the output buffer is the name of the output handler that is called when it is flushed.

Eg

ob_start('my_buffer');


function my_buffer($buff){
    return str_replace(":)","<img src=\"smiley\"/>",$buff);
}
andrew
  • 9,313
  • 7
  • 30
  • 61
  • Thank you, that works. I wonder why there is no mention of this in the documentation whatsoever. – Kalko Jun 20 '16 at 13:17