mirror of
https://github.com/php/php-src.git
synced 2026-03-24 00:02:20 +01:00
Deprecate returning non-string values from a user output handler (#18932)
https://wiki.php.net/rfc/deprecations_php_8_4
This commit is contained in:
@@ -5,7 +5,7 @@ Bug #60768 Output buffer not discarded
|
||||
|
||||
global $storage;
|
||||
|
||||
ob_start(function($buffer) use (&$storage) { $storage .= $buffer; }, 20);
|
||||
ob_start(function($buffer) use (&$storage) { $storage .= $buffer; return ''; }, 20);
|
||||
|
||||
echo str_repeat("0", 20); // fill in the buffer
|
||||
|
||||
|
||||
@@ -35,19 +35,24 @@ foreach ($callbacks as $callback) {
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
--EXPECTF--
|
||||
--> Use callback 'return_empty_string':
|
||||
|
||||
|
||||
--> Use callback 'return_false':
|
||||
|
||||
Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_false is deprecated in %s on line %d
|
||||
My output.
|
||||
|
||||
--> Use callback 'return_null':
|
||||
|
||||
Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_null is deprecated in %s on line %d
|
||||
|
||||
|
||||
--> Use callback 'return_string':
|
||||
I stole your output.
|
||||
|
||||
--> Use callback 'return_zero':
|
||||
0
|
||||
|
||||
Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_zero is deprecated in %s on line %d
|
||||
0
|
||||
|
||||
147
tests/output/ob_start_callback_bad_return/exception_handler.phpt
Normal file
147
tests/output/ob_start_callback_bad_return/exception_handler.phpt
Normal file
@@ -0,0 +1,147 @@
|
||||
--TEST--
|
||||
ob_start(): Check behaviour with deprecation converted to exception
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
class NotStringable {
|
||||
public function __construct(public string $val) {}
|
||||
}
|
||||
class IsStringable {
|
||||
public function __construct(public string $val) {}
|
||||
public function __toString() {
|
||||
return __CLASS__ . ": " . $this->val;
|
||||
}
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
function return_non_stringable($string) {
|
||||
global $log;
|
||||
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
|
||||
return new NotStringable($string);
|
||||
}
|
||||
|
||||
function return_stringable($string) {
|
||||
global $log;
|
||||
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
|
||||
return new IsStringable($string);
|
||||
}
|
||||
|
||||
$cases = [
|
||||
'return_null',
|
||||
'return_false',
|
||||
'return_true',
|
||||
'return_zero',
|
||||
'return_non_stringable',
|
||||
'return_stringable',
|
||||
];
|
||||
foreach ($cases as $case) {
|
||||
$log = [];
|
||||
echo "\n\nTesting: $case\n";
|
||||
ob_start($case);
|
||||
echo "Inside of $case\n";
|
||||
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_false
|
||||
ErrorException: 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
|
||||
>>>
|
||||
|
||||
Testing: return_non_stringable
|
||||
ErrorException: ob_end_flush(): Returning a non-string result from user output handler return_non_stringable is deprecated in %s:%d
|
||||
Stack trace:
|
||||
#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, 69)
|
||||
#1 %s(%d): ob_end_flush()
|
||||
#2 {main}
|
||||
|
||||
End of return_non_stringable, log was:
|
||||
return_non_stringable: <<<Inside of return_non_stringable
|
||||
>>>
|
||||
|
||||
Testing: return_stringable
|
||||
ErrorException: ob_end_flush(): Returning a non-string result from user output handler return_stringable is deprecated in %s:%d
|
||||
Stack trace:
|
||||
#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, 69)
|
||||
#1 %s(%d): ob_end_flush()
|
||||
#2 {main}
|
||||
|
||||
End of return_stringable, log was:
|
||||
return_stringable: <<<Inside of return_stringable
|
||||
>>>
|
||||
@@ -0,0 +1,143 @@
|
||||
--TEST--
|
||||
ob_start(): Check behaviour with deprecation converted to exception
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
class NotStringable {
|
||||
public function __construct(public string $val) {}
|
||||
}
|
||||
class IsStringable {
|
||||
public function __construct(public string $val) {}
|
||||
public function __toString() {
|
||||
return __CLASS__ . ": " . $this->val;
|
||||
}
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
function return_non_stringable($string) {
|
||||
global $log;
|
||||
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
|
||||
return new NotStringable($string);
|
||||
}
|
||||
|
||||
function return_stringable($string) {
|
||||
global $log;
|
||||
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
|
||||
return new IsStringable($string);
|
||||
}
|
||||
|
||||
ob_start('return_null');
|
||||
ob_start('return_false');
|
||||
ob_start('return_true');
|
||||
ob_start('return_zero');
|
||||
ob_start('return_non_stringable');
|
||||
ob_start('return_stringable');
|
||||
|
||||
echo "In all of them\n\n";
|
||||
try {
|
||||
ob_end_flush();
|
||||
} catch (\ErrorException $e) {
|
||||
echo $e->getMessage() . "\n";
|
||||
}
|
||||
echo "Ended return_stringable handler\n\n";
|
||||
|
||||
try {
|
||||
ob_end_flush();
|
||||
} catch (\ErrorException $e) {
|
||||
echo $e->getMessage() . "\n";
|
||||
}
|
||||
echo "Ended return_non_stringable handler\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_stringable: <<<In all of them
|
||||
|
||||
>>>
|
||||
return_non_stringable: <<<ob_end_flush(): Returning a non-string result from user output handler return_stringable is deprecated
|
||||
Ended return_stringable handler
|
||||
|
||||
>>>
|
||||
return_zero: <<<ob_end_flush(): Returning a non-string result from user output handler return_non_stringable is deprecated
|
||||
Ended return_non_stringable handler
|
||||
|
||||
>>>
|
||||
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
|
||||
|
||||
>>>
|
||||
@@ -0,0 +1,23 @@
|
||||
--TEST--
|
||||
ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns false)
|
||||
--INI--
|
||||
memory_limit=2M
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
ob_start(function() {
|
||||
// We are out of memory, now trigger a deprecation
|
||||
return false;
|
||||
});
|
||||
|
||||
$a = [];
|
||||
// trigger OOM in a resize operation
|
||||
while (1) {
|
||||
$a[] = 1;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d
|
||||
|
||||
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
|
||||
@@ -0,0 +1,30 @@
|
||||
--TEST--
|
||||
ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns stringable object)
|
||||
--INI--
|
||||
memory_limit=2M
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
class IsStringable {
|
||||
public function __construct(public string $val) {}
|
||||
public function __toString() {
|
||||
return __CLASS__ . ": " . $this->val;
|
||||
}
|
||||
}
|
||||
|
||||
ob_start(function() {
|
||||
// We are out of memory, now trigger a deprecation
|
||||
return new IsStringable("");
|
||||
});
|
||||
|
||||
$a = [];
|
||||
// trigger OOM in a resize operation
|
||||
while (1) {
|
||||
$a[] = 1;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d
|
||||
|
||||
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
|
||||
@@ -0,0 +1,32 @@
|
||||
--TEST--
|
||||
ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns non-stringable object)
|
||||
--INI--
|
||||
memory_limit=2M
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
class NotStringable {
|
||||
public function __construct(public string $val) {}
|
||||
}
|
||||
|
||||
ob_start(function() {
|
||||
// We are out of memory, now trigger a deprecation
|
||||
return new NotStringable("");
|
||||
});
|
||||
|
||||
$a = [];
|
||||
// trigger OOM in a resize operation
|
||||
while (1) {
|
||||
$a[] = 1;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d
|
||||
|
||||
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
|
||||
|
||||
Fatal error: Uncaught Error: Object of class NotStringable could not be converted to string in %s:%d
|
||||
Stack trace:
|
||||
#0 {main}
|
||||
thrown in %s on line %d
|
||||
@@ -0,0 +1,23 @@
|
||||
--TEST--
|
||||
ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns true)
|
||||
--INI--
|
||||
memory_limit=2M
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
ob_start(function() {
|
||||
// We are out of memory, now trigger a deprecation
|
||||
return true;
|
||||
});
|
||||
|
||||
$a = [];
|
||||
// trigger OOM in a resize operation
|
||||
while (1) {
|
||||
$a[] = 1;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d
|
||||
|
||||
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
|
||||
@@ -0,0 +1,23 @@
|
||||
--TEST--
|
||||
ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns zero)
|
||||
--INI--
|
||||
memory_limit=2M
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
ob_start(function() {
|
||||
// We are out of memory, now trigger a deprecation
|
||||
return 0;
|
||||
});
|
||||
|
||||
$a = [];
|
||||
// trigger OOM in a resize operation
|
||||
while (1) {
|
||||
$a[] = 1;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d
|
||||
|
||||
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
|
||||
@@ -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: <<<>>>
|
||||
Reference in New Issue
Block a user