Skip to content

Instantly share code, notes, and snippets.

@md2perpe
Created May 9, 2012 15:14
Show Gist options
  • Save md2perpe/2645350 to your computer and use it in GitHub Desktop.
Save md2perpe/2645350 to your computer and use it in GitHub Desktop.
Making a destructor to be run even on fatal error
<?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