note 106020 added to exception.getprevious
| From: | Anonymous at osu1 dot php dot net | Date: | Mon, 03 Oct 2011 20:11:11 +0000 |
| Subject: | note 106020 added to exception.getprevious | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183618@lists.php.net to get a copy of this message | ||
May be I am late (5.2 is not supported). But if you want use the functionality of "previous
Exception" in PHP < 5.3 or write compatible code, you can use next way as below.
<?php
abstract class App_Exception_PreviousNativeAbstract extends Exception {
public static $printPrevious = true;
public function __toString() {
$result = array();
$result[] = sprintf("Exception '%s' with message '(%s) %s' in
%s:%d", get_class($this), $this->code, $this->message, $this->file, $this->line);
$result[] = '---[Stack trace]:';
$result[] = $this->getTraceAsString();
if (self::$printPrevious) {
$previous = $this->getPrevious();
if ($previous) {
do {
$result[] = '---[Previous exception]:';
$result[] = sprintf("Exception '%s' with message '(%s)
%s' in %s:%d", get_class($previous), $previous->getCode(), $previous->getMessage(),
$previous->getFile(), $previous->getLine());
$result[] = '---[Stack trace]:';
$result[] = $previous->getTraceAsString();
} while(method_exists($previous, 'getPrevious') && ($previous =
$previous->getPrevious()));
}
}
return implode("\r\n", $result);
}
}
abstract class App_Exception_PreviousLegacyAbstract extends App_Exception_PreviousNativeAbstract {
protected $previous;
public function __construct($message, $code = 0, Exception $previous = null) {
$this->previous = $previous;
parent::__construct($message, $code);
}
public function getPrevious() {
return $this->previous;
}
}
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
abstract class App_Exception_PreviousAbstract extends App_Exception_PreviousNativeAbstract {}
}
else {
abstract class App_Exception_PreviousAbstract extends App_Exception_PreviousLegacyAbstract {}
}
class App_Exception extends App_Exception_PreviousAbstract {
public function __construct($message, $code = 0, Exception $previous = null) {
parent::__construct($message, 0, $previous);
}
}
// Example:
try {
// ...
throw new Exception('Exception mesage');
// ...
} catch (Exception $e) {
throw new App_Exception('App exception mesage', 0, $e);
}
?>
----
Server IP: 77.88.252.46
Probable Submitter: 195.225.156.176
----
Manual Page -- http://www.php.net/manual/en/exception.getprevious.php
Edit -- https://master.php.net/note/edit/106020
Del: integrated -- https://master.php.net/note/delete/106020/integrated
Del: useless -- https://master.php.net/note/delete/106020/useless
Del: bad code -- https://master.php.net/note/delete/106020/bad+code
Del: spam -- https://master.php.net/note/delete/106020/spam
Del: non-english -- https://master.php.net/note/delete/106020/non-english
Del: in docs -- https://master.php.net/note/delete/106020/in+docs
Del: other reasons-- https://master.php.net/note/delete/106020
Reject -- https://master.php.net/note/reject/106020
Search -- https://master.php.net/manage/user-notes.php