PHP: ob_start () how to restrict callback

How to end a callback call ob_starton a call *_clean().

ob_start(function($buffer, $phase){
    // code here
}, 0, PHP_OUTPUT_HANDLER_FLUSHABLE | PHP_OUTPUT_HANDLER_REMOVABLE);

Does not prevent calls from starting ob_end_clean, ob_get_cleanor ob_clean. I expect a notification that the buffer has not been started with the correct flag PHP_OUTPUT_HANDLER_CLEANABLE according to the docs .

As for the constants PHP_OUTPUT_HANDLER_*, I did not find a suitable man page where the parameter is explained $phaseand the groups of bits related to these constants are detailed. Even the actual names / values ​​that I had to get from the global CONSTANTS variable.

PHP_OUTPUT_HANDLER_START
PHP_OUTPUT_HANDLER_WRITE
PHP_OUTPUT_HANDLER_FLUSH
PHP_OUTPUT_HANDLER_CLEAN
PHP_OUTPUT_HANDLER_FINAL
PHP_OUTPUT_HANDLER_CONT
PHP_OUTPUT_HANDLER_END
PHP_OUTPUT_HANDLER_CLEANABLE
PHP_OUTPUT_HANDLER_FLUSHABLE
PHP_OUTPUT_HANDLER_REMOVABLE
PHP_OUTPUT_HANDLER_STDFLAGS
PHP_OUTPUT_HANDLER_STARTED
PHP_OUTPUT_HANDLER_DISABLED

, , . $phase ( printf, echo, ob_start ).

, , :

  • , , :

    for ($i = 0; $i < ob_get_level(); $i++) { $final .= ob_get_clean(); }

  • , , /

  • , ,

:

  • ​​?
  • ?
+6
1

, , - ob_ *. .

:

<?php

class MyBuffer
{
    static $buffer = '';

    public static function add(string $output)
    {
        self::$buffer .= $output;
    }

    public static function get()
    {
        return self::$buffer;
    }

    public static function get_clean()
    {
        $buffer = self::$buffer;
        self::$buffer = '';
        return $buffer;
    }
}

// ... some code
MyBuffer::add('Hello user, glad you joined us!');
// ... some more code
MyBuffer::add('Your socore is ' . $score . '. Congratz!');
// ...
echo MyBuffer::get_clean();
0

Source: https://habr.com/ru/post/1015729/


All Articles