PHP 8.5: Emitting output from custom output buffer handlers is deprecated
The ob_start
PHP function provides a way to start a user-provided output buffer handler function, and return the contents of that buffer processed by that function. The buffer handler functions should not emit any output on their own, and they are not allowed to start a buffer from within the handler function.
If a user-provided output buffer handler emits output of their own, that output is silently ignored in PHP versions prior to PHP 8.5. Since PHP 8.5, user-provided output buffer handlers that emit output on their own (e.g. print
calls), PHP emits a deprecation warning; The output continues to be hidden.
ob_start(
static function(string $buffer, int $phase): string {
echo "test"; // Emits output, deprecated, output still hidden
return (str_replace("hunter2", "****", $buffer));
}
);
echo "hunter2 test";
ob_end_flush();
Deprecated: ob_end_flush(): Producing output from user output handler ... is deprecated in ... on line ...
Recommended Replacement
Output buffer handlers should not emit outputs of their own. Instead, they can modify the incoming buffer contents to include any additional information they might want to include.
Related Changes
Backward Compatibility Impact
Emitting outputs from within a custom output buffer handler now emits a PHP deprecation notice. The output continues to be hidden, which is the same behavior across all PHP versions.
In PHP 9.0 and later, this might start to throw a Fatal error.