php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29685 Destructor is called to late, unset do not work
Submitted: 2004-08-15 01:28 UTC Modified: 2004-08-15 16:09 UTC
From: sampw at hotmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: sampw at hotmail dot com
New email:
PHP Version: OS:

 

 [2004-08-15 01:28 UTC] sampw at hotmail dot com
Description:
------------
Destructor is not called to late. If you use a singleton pattern unset does not work, because there exist a private static reference for the object.

Why is the destructor called so unreliable?

Is such a singleton pattern not possible in PHP?

Or must the static $instance be public, inorder to unset it manually? (Not a nice solution?!)

Reproduce code:
---------------
class Session { //Db Session (Singleton)

    static private $instance = false;

    private function __construct() { }

    public function __destruct() {
        $this->sessionWriteClose(); // e.G. store session data
    }

    static function instance() {
        if(!Session::$instance)
            Session::$instance = new Session();
        return Session::$instance;
    }
}

$OBJ_SESSION = Session::instance();
unset($OBJ_SESSION); // Does not work, because of the static private $instance


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-15 15:10 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The dtor is called during shutdown. I don\'t see the slightest problem here.

 [2004-08-15 15:56 UTC] sampw at hotmail dot com
The dtor is called during shutdown. Yes, but to late!
In other bug reports, you called it a feature to use unset at the end of a script inorder to get a reliable working destructor. Because otherwise functions like $this->sessionWriteClose();, which rely on other objects doesn't work reliable. That works fine, if you doesn't use a classical singleton implementation. Because here unset does not work (because there exist a private static reference inside the object!), which means that the destructor doesn't work correctly ($this->sessionWriteClose() works not!).

If you said that, that isn't a bug, I have to suggest that a classical singleton implementation with a reliable working destructor is not possible in php?? Sounds poor ...
 [2004-08-15 16:09 UTC] helly@php.net
Well i must admit i haven't tried with sessions yet. They are closed during R-shutdown pahse and at least theoretically dtor's should be called kust before R-shutdown begins. If that is not the case we must either fix ext/session or something else. Until that is clear you need an explicitly shutdown function for your singleton (one that unsets the static member).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 13:01:27 2024 UTC