php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30447 Static functions called from a destructor don't have access to the object
Submitted: 2004-10-15 16:11 UTC Modified: 2005-01-14 22:23 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:3 (100.0%)
From: arend at auton dot nl Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
 [2004-10-15 16:11 UTC] arend at auton dot nl
Description:
------------
If you use singleton classes, it is often handy to have one global instance of your class and manipulate this instance using static functions. Then it can also be handy to call such a static function for cleanup purposes inside the destructor. However, if the object is cleaned up using unset(), the static functions lose access to the global instance before the destructor is finished. If the instance just runs out of scope, it works normally however.

Btw, this bug seems somewhat similar to Bug #29685 in which it also appears the destructor is called too late with a singleton pattern, with the exception that this code does not have a private instance and simply uses a global instance.

Reproduce code:
---------------
class Test
{
        public function __construct() { $this->a = "test"; }
        public function __destruct() { self::printA(); }

        static public function printA() { global $test; echo "$test->a\n"; }

        private $a;
}

$test = new Test;
unset($test);

Expected result:
----------------
It should print: "test".

Actual result:
--------------
It prints nothing.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-13 01:20 UTC] tony2001@php.net
No bug here:

"Because static methods are callable without an instance of the object created, the pseudo variable $this is not available inside the method declared as static."
http://php.net/manual/en/language.oop5.static.php
 [2005-01-13 12:13 UTC] arend at auton dot nl
I didn't complain about $this not being available, but about a global reference to the class not being available. This global  reference *is* available when the variable runs out of scope, but it's not available when explicitly deleted using unset(). This way, the current behavior is simply not consistent. To fix this, it would be best if the global reference was kept intact until *after* the destructor has finished.
 [2005-01-14 22:23 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

We are not ensuring any order of shutdown.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Dec 15 18:00:01 2025 UTC