note 130560 added to function.register-shutdown-function
| From: | Anonymous | Date: | Sat, 29 Nov 2025 19:11:10 +0000 |
| Subject: | note 130560 added to function.register-shutdown-function | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-222438@lists.php.net to get a copy of this message | ||
Here is a great example of using shutdown.
I am using a database class for 20 years now. Starting with mysql_connect of course. then
transformed it to mysqli as time went by. It was a real transition at some point.
It recently came to my attention that die() and exit() will not handle mysql_close() $con->close
properly. __destruct might not be called.
so a good way around this is register the shutdown function so mysql will always close.
function __destruct(){
$this->close();
}
function close() {
// this function closes the database
if($this->connected){
if($this->use_mysqli){
$this->connected = false;
return $this->con->close();
} else {
$this->connected = false;
return mysql_close($this->con);
}
}
}
function __construct($host, $user, $pass, $db){
global $_CONFIG;
if(isset($_CONFIG['use_mysql']) && $_CONFIG['use_mysql']){
$this->use_mysqli = false;
} else {
$this->use_mysqli = function_exists("mysqli_connect");
}
$this->database($host, $user, $pass, $db);
register_shutdown_function([$this, 'close']);
}
By Jacob Slomp
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 162.157.229.111)
----
Manual Page -- https://php.net/manual/en/function.register-shutdown-function.php
Edit -- https://main.php.net/note/edit/130560
Del: integrated -- https://main.php.net/note/delete/130560/integrated
Del: useless -- https://main.php.net/note/delete/130560/useless
Del: bad code -- https://main.php.net/note/delete/130560/bad+code
Del: spam -- https://main.php.net/note/delete/130560/spam
Del: non-english -- https://main.php.net/note/delete/130560/non-english
Del: in docs -- https://main.php.net/note/delete/130560/in+docs
Del: other reasons-- https://main.php.net/note/delete/130560
Reject -- https://main.php.net/note/reject/130560
Search -- https://main.php.net/manage/user-notes.php