Re: destructor not called for static members?
| From: | Jochem Maas | Date: | Thu, 10 Mar 2005 21:12:18 +0000 |
| Subject: | Re: destructor not called for static members? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-210474@lists.php.net to get a copy of this message | ||
Richard Lynch wrote:
self::$_ref is completely independent of any instance of that class. but the rest of what you say is correct AFAIK - basically by the time PHP gets round to cleaning up the class from the 'program space' there is nothing to 'talk' to regardless. but anyway a quick demonstration which might prove helpful: <?php class c1 { public function __destruct() { echo "__destruct"; } } class c2 { private static $_ref; private static $_selfCnt = 0; public function __construct() { self::$_selfCnt++; if (self::$_selfCnt == 1) { self::$_ref = new c1(); } $this->_ref = new c1(); } public function __destruct() { self::$_selfCnt++; if (!self::$_selfCnt) { // the last samurai has fallen unset(self::$_ref); } } } $obj = new c2(); $obj2 = new c2(); unset($obj,$obj2); ?> gives me triple "__destruct".<?php class c1 { public function __destruct() {Another take on this: PHP can't destroy $_ref until the entire script, even with aute_append files, is completely done and gone, because you *MIGHT* make another c2() somewhere.echo '__destruct';} } class c2 { private static $_ref; public function __construct() {self::$_ref = new c1();//$this->_ref = new c1();} } $obj = new c2(); unset($obj); ?> i thought this should display __destruct in both cases from c2::__construct, but only the one with non static access seems to call c1::destruct. and if i remove unsetting $obj from the end of code this one also doesn`t work. is it expected behaviour? or maybe when __destructor is called after script finishes execution than output is no longer possible? i tried also send some information to file and this also didn`t work. help please :)
It's quite likely that your c1 *IS* getting destroyed, but long after any output you can see, because STDOUT and STDERR are already shut down. Change your echo statements to do an error_log() and you just might see more things going on "behind the scenes" -- No promise though. PHP might not be calling the destructors at that point, as a general script cleanup might just be free()-ing big chunks of RAM instead of doing a full descructor call.