Created
May 9, 2012 15:14
-
-
Save md2perpe/2645350 to your computer and use it in GitHub Desktop.
Making a destructor to be run even on fatal error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Logger | |
{ | |
protected $rows = array(); | |
public function __construct() | |
{ | |
register_shutdown_function(array($this, 'shutdown')); | |
} | |
public function shutdown() | |
{ | |
$last_error = error_get_last(); | |
if ($last_error['type'] == 1) | |
{ | |
$this->log("A fatal error occurred on line {$last_error['line']} in file '{$last_error['file']}'. Shutting down..."); | |
$this->__destruct(); | |
} | |
} | |
public function __destruct() | |
{ | |
$this->save(); | |
} | |
public function log($row) | |
{ | |
$this->rows[] = $row; | |
} | |
public function save() | |
{ | |
echo '<ul>'; | |
foreach ($this->rows as $row) | |
{ | |
echo '<li>', $row, '</li>'; | |
} | |
echo '</ul>'; | |
} | |
} | |
$logger = new Logger; | |
$logger->log('Before'); | |
$nonset->foo(); | |
$logger->log('After'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment