Skip to content

Deprecate returning non-string values from a user output handler #18932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Tests
  • Loading branch information
DanielEScherzer committed Jun 24, 2025
commit 720923ec809bb31e74e769b63f6b9bf4a3d9b95c
1 change: 1 addition & 0 deletions sapi/cli/tests/gh8827-003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ file_put_contents('php://fd/2', "Goes to stderrFile\n");

ob_start(function ($buffer) use ($stdoutStream) {
fwrite($stdoutStream, $buffer);
return '';
}, 1);

print "stdoutFile:\n";
Expand Down
91 changes: 91 additions & 0 deletions tests/output/ob_start_callback_bad_return/exception_handler.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
--TEST--
ob_start(): Check behaviour with deprecation converted to exception
--FILE--
<?php

$log = [];

set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
});

function return_null($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return null;
}

function return_false($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return false;
}

function return_true($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return true;
}

function return_zero($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return 0;
}

$cases = ['return_null', 'return_false', 'return_true', 'return_zero'];
foreach ($cases as $case) {
$log = [];
echo "\n\nTesting: $case\n";
ob_start($case);
echo "Inside of $case";
try {
ob_end_flush();
} catch (\ErrorException $e) {
echo $e . "\n";
}
echo "\nEnd of $case, log was:\n";
echo implode("\n", $log);
}

?>
--EXPECTF--
Testing: return_null
ErrorException: ob_end_flush(): Returning a non-string result from user output handler return_null is deprecated in %s:%d
Stack trace:
#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, %d)
#1 %s(%d): ob_end_flush()
#2 {main}

End of return_null, log was:
return_null: <<<Inside of return_null>>>

Testing: return_false
Inside of return_falseErrorException: ob_end_flush(): Returning a non-string result from user output handler return_false is deprecated in %s:%d
Stack trace:
#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, %d)
#1 %s(%d): ob_end_flush()
#2 {main}

End of return_false, log was:
return_false: <<<Inside of return_false>>>

Testing: return_true
ErrorException: ob_end_flush(): Returning a non-string result from user output handler return_true is deprecated in %s:%d
Stack trace:
#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, %d)
#1 %s(%d): ob_end_flush()
#2 {main}

End of return_true, log was:
return_true: <<<Inside of return_true>>>

Testing: return_zero
0ErrorException: ob_end_flush(): Returning a non-string result from user output handler return_zero is deprecated in %s:%d
Stack trace:
#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, %d)
#1 %s(%d): ob_end_flush()
#2 {main}

End of return_zero, log was:
return_zero: <<<Inside of return_zero>>>
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
--TEST--
ob_start(): Check behaviour with deprecation converted to exception
--FILE--
<?php

$log = [];

set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
});

function return_null($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return null;
}

function return_false($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return false;
}

function return_true($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return true;
}

function return_zero($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return 0;
}

ob_start('return_null');
ob_start('return_false');
ob_start('return_true');
ob_start('return_zero');

echo "In all of them\n\n";
try {
ob_end_flush();
} catch (\ErrorException $e) {
echo $e->getMessage() . "\n";
}
echo "Ended return_zero handler\n\n";

try {
ob_end_flush();
} catch (\ErrorException $e) {
echo $e->getMessage() . "\n";
}
echo "Ended return_true handler\n\n";

try {
ob_end_flush();
} catch (\ErrorException $e) {
echo $e->getMessage() . "\n";
}
echo "Ended return_false handler\n\n";

try {
ob_end_flush();
} catch (\ErrorException $e) {
echo $e->getMessage() . "\n";
}
echo "Ended return_null handler\n\n";

echo "All handlers are over\n\n";
echo implode("\n", $log);

?>
--EXPECT--
ob_end_flush(): Returning a non-string result from user output handler return_null is deprecated
Ended return_null handler

All handlers are over

return_zero: <<<In all of them

>>>
return_true: <<<0ob_end_flush(): Returning a non-string result from user output handler return_zero is deprecated
Ended return_zero handler

>>>
return_false: <<<ob_end_flush(): Returning a non-string result from user output handler return_true is deprecated
Ended return_true handler

>>>
return_null: <<<ob_end_flush(): Returning a non-string result from user output handler return_true is deprecated
Ended return_true handler

ob_end_flush(): Returning a non-string result from user output handler return_false is deprecated
Ended return_false handler

>>>
89 changes: 89 additions & 0 deletions tests/output/ob_start_callback_bad_return/multiple_handlers.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
--TEST--
ob_start(): Check behaviour with multiple nested handlers with had return values
--FILE--
<?php

$log = [];

function return_given_string($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return $string;
}

function return_empty_string($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return "";
}

function return_false($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return false;
}

function return_true($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return true;
}

function return_null($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return null;
}

function return_string($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return "I stole your output.";
}

function return_zero($string) {
global $log;
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
return 0;
}

ob_start('return_given_string');
ob_start('return_empty_string');
ob_start('return_false');
ob_start('return_true');
ob_start('return_null');
ob_start('return_string');
ob_start('return_zero');

echo "Testing...";

ob_end_flush();
ob_end_flush();
ob_end_flush();
ob_end_flush();
ob_end_flush();
ob_end_flush();
ob_end_flush();

echo "\n\nLog:\n";
echo implode("\n", $log);
?>
--EXPECTF--
Log:
return_zero: <<<Testing...>>>
return_string: <<<
Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_zero is deprecated in %s on line %d
0>>>
return_null: <<<I stole your output.>>>
return_true: <<<
Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_null is deprecated in %s on line %d
>>>
return_false: <<<
Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_true is deprecated in %s on line %d
>>>
return_empty_string: <<<
Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_false is deprecated in %s on line %d

Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_true is deprecated in %s on line %d
>>>
return_given_string: <<<>>>
Loading