mirror of
https://github.com/php/php-src.git
synced 2024-12-15 12:54:57 +08:00
55dd3945fb
We specify that the return type of Exception::getMessage() is a string. However, we don't currently ensure this, because Exception::$message is a protected member that can be set to any type. Fix this by performing an explicit type-cast. This also requires a temporary refcount increment in the __toString() object handler, because there is no additional owner of the object, and it may get released prematurely as part of the __toString() call.
42 lines
1.0 KiB
PHP
42 lines
1.0 KiB
PHP
--TEST--
|
|
Exception with delayed message computation
|
|
--FILE--
|
|
<?php
|
|
|
|
class MyException extends Exception {
|
|
public $message;
|
|
public $messageCallback;
|
|
|
|
public function __construct() {
|
|
$this->messageCallback = static function() {
|
|
return "Foobar";
|
|
};
|
|
$this->message = new class($this->message, $this->messageCallback) {
|
|
private $message;
|
|
private $messageCallback;
|
|
|
|
public function __construct(&$message, &$messageCallback)
|
|
{
|
|
$this->message = &$message;
|
|
$this->messageCallback = &$messageCallback;
|
|
}
|
|
|
|
public function __toString(): string
|
|
{
|
|
$messageCallback = $this->messageCallback;
|
|
$this->messageCallback = null;
|
|
return $this->message = $messageCallback();
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
throw new MyException;
|
|
|
|
?>
|
|
--EXPECTF--
|
|
Fatal error: Uncaught MyException: Foobar in %s:%d
|
|
Stack trace:
|
|
#0 {main}
|
|
thrown in %s on line %d
|