note 117547 deleted from language.oop5.decon by cmb
| From: | cmb@php.net | Date: | Sun, 08 Sep 2019 10:23:17 +0000 |
| Subject: | note 117547 deleted from language.oop5.decon by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212134@lists.php.net to get a copy of this message | ||
Note Submitter: 479258741 at qq dot com
----
If a new reference of the object is created during __destruct, the memory for the object will not be
freed, and the __destruct will not be called for a second time.
<?php
class Foo{
public $i = "I=12345678\n";
public function showI(){
echo($this->i);
}
public function __destruct(){
echo("in __destruct\n");
if($GLOBALS['flag'])
$GLOBALS['test'][0] = $this;
}
}
$flag = true;
$test = [new Foo()];
echo("deleting Foo\n");
unset($test[0]);
echo("deleted Foo\n");
$test[0]->showI();
$flag = false;
echo("re-deleting Foo\n");
unset($test[0]);
echo("deleted Foo\n");
?>
In the above example, the output is:
deleting Foo
in __destruct
deleted Foo
I=12345678
re-deleting Foo
deleted Foo
so the __destruct was called only once and the memory was not freed.