note 77919 deleted from language.oop5.decon by tularis
| From: | tularis@php.net | Date: | Thu, 18 Oct 2007 14:18:32 +0000 |
| Subject: | note 77919 deleted from language.oop5.decon by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-131500@lists.php.net to get a copy of this message | ||
Note Submitter: Typer85 at gmail dot com
----
Some new programmers to PHP might not be familiar on how to manually call an object's
destructor at any time during a script's execution.
Let's assume I have the following class:
<?php
class Test
{
public function __destruct( ) {
echo 'Gone';
}
}
?>
Now let's assume I create ONE object of that class:
<?php
$Object = new Test( );
?>
The object exists throughout the duration of the script's execution. The destructor will be
called explicitly by PHP since PHP will automatically delete the object to clean up resources at the
end of the script's execution.
This is all fine and dandy, but now let's assume I create the object and than destroy it
manually:
<?php
$Object = new Test( );
// Delete Object.
unset( $Object);
?>
It is safe to assume that since only one object of that class exists and I manually destroyed it
that the destructor will be called only once when I destroy it and not when the script finishes
execution.
So in short, to manually call an object's destructor you use the unset( ) function.