note 108598 added to language.oop5.decon

From: Date: Wed, 09 May 2012 13:57:04 +0000
Subject: note 108598 added to language.oop5.decon
Groups: php.notes 
Request: Send a blank email to php-notes+get-187548@lists.php.net to get a copy of this message
As of PHP 5.3.10 destructors are not run on shutdown caused by fatal errors. For example: <?php class Logger { protected $rows = array(); 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'); ?> Without the $nonset->foo(); line, Before and After will both be printed, but with the line neither will be printed. One can however register the destructor or another method as a shutdown function: <?php class Logger { protected $rows = array(); public function __construct() { register_shutdown_function(array($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'); ?> Now Before will be printed, but not After, so you can see that a shutdown occurred after Before. ---- Server IP: 80.67.10.227 Probable Submitter: 80.252.162.18 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php Edit -- https://master.php.net/note/edit/108598 Del: integrated -- https://master.php.net/note/delete/108598/integrated Del: useless -- https://master.php.net/note/delete/108598/useless Del: bad code -- https://master.php.net/note/delete/108598/bad+code Del: spam -- https://master.php.net/note/delete/108598/spam Del: non-english -- https://master.php.net/note/delete/108598/non-english Del: in docs -- https://master.php.net/note/delete/108598/in+docs Del: other reasons-- https://master.php.net/note/delete/108598 Reject -- https://master.php.net/note/reject/108598 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#187548) next »