note 78737 deleted from language.oop5.decon by cmb
| From: | cmb@php.net | Date: | Mon, 18 Mar 2019 10:20:25 +0000 |
| Subject: | note 78737 deleted from language.oop5.decon by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210401@lists.php.net to get a copy of this message | ||
Note Submitter: James Laver
----
I recently found, while implementing a database backed session class, that PHP has an apparently
less than desirably structured destruction order. Basically, my session class which would have saved
when destructed, was being destructed after one of the classes it depends on. Apparently we cannot,
therefore depend on PHP to use reverse initialisation order.
My solution was to use register_shutdown_function, which is called before any objects are killed on
script end.
Quick example (if you're going to use it, I recommend tidying up the code somewhat) :
<?php
class Session
function __construct()
{
//Initialise the session here...
//Register the shutdown function
register_shutdown_function(array($this,"save"));
}
function save()
{
//Save it here
}
function __destruct()
{
//Persisting the session here will not work.
}
?>