Destructors in PHP

By: Andi, Stig and Derick Emailed: 1646 times Printed: 2115 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Destructor functions are the opposite of constructors. They are called when the object is being destroyed (for example, when there are no more references to the object). As PHP makes sure all resources are freed at the end of each request, the importance of destructors is limited. However, they can still be useful for performing certain actions, such as flushing a resource or logging information on object destruction. There are two situations where your destructor might be called: during your script’s execution when all references to an object are destroyed, or when the end of the script is reached and PHP ends the request. The latter situation is delicate because you are relying on some objects that might already have had their destructors called and are not accessible anymore. So, use it with care, and don’t rely on other objects in your destructors.

Defining a destructor is as simple as adding a __destruct() method to your class:

class MyClass {

function __destruct()

{

print "An object of type MyClass is being destroyed\n";

}

}

$obj = new MyClass();

$obj = NULL;

This script prints An object of type MyClass is being destroyed In this example, when $obj = NULL; is reached, the only handle to the object is destroyed, and therefore the destructor is called, and the object itself is destroyed. Even without the last line, the destructor would be called, but it would be at the end of the request during the execution engine’s shutdown.

Tip: The exact point in time of the destructor being called is not guaranteed by PHP, and it might be a few statements after the last reference to the object has been released. Thus, be aware not to write your application in a way where this could hurt you.


PHP Home | All PHP Tutorials | Latest PHP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Andi, Stig and Derick
Execution Lifetime of a PHP script
preg_split() and explode() in PHP
preg_replace() and preg_replace_callback() in PHP
preg_match(), function preg_match_all(), preg_grep() in PHP
tmpfile() in PHP
Renaming and Removing Files in PHP
Locking files in PHP
File Handling in PHP
Handling BLOB in PHP and MySQL
Using Sessions in PHP
Using Cookies in PHP
Using PEAR::Crypt_HMAC in PHP
Using HMAC Verification in PHP
Input Validation in PHP
__autoload() METHOD in PHP

More Tutorials in PHP
PHP code to import from CSV file to MySQL
PHP code to write to a CSV file from MySQL query
PHP code to write to a CSV file for Microsoft Applications
Convert XML to CSV in PHP
Password must include both numeric and alphabetic characters - Magento
PHP file upload (Large Files)
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Error: Length parameter must be greater than 0
Multiple File Upload in PHP using IFRAME
Resume or Pause File Uploads in PHP
Exception in module wampmanager.exe at 000F15A0 in Windows 8
Handling file locks in PHP
HTML table output using Nested for loops in PHP
Count occurrences of a character in a String in PHP

More Latest News
Most Viewed Articles (in PHP )
isset() function in PHP
Convert XML to CSV in PHP
PHP code to write to a CSV file for Microsoft Applications
preg_replace() and preg_replace_callback() in PHP
A Basic Example using PHP in AWS (Amazon Web Services)
Function to return number of digits of an integer in PHP
Password must include both numeric and alphabetic characters - Magento
for Loops in PHP
public, protected, and private Properties in PHP
Handling BLOB in PHP and MySQL
preg_split() and explode() in PHP
Removing Duplicate Elements from an Array in PHP
PHP ./configure RESULTING IN __rcp_thread_destroy@GLIBC_2_2_3_... AND UNRESOLVED REFERENCES WITH ORACLE OCI8
Perl's Encoding::FixLatin equivalent in PHP
Anonymous functions as properties in Classes in PHP
Most Emailed Articles (in PHP)
PHP code to write to a CSV file from MySQL query
Comparison operators in PHP
GDBM, NDBM, DB2, DB3, DBM, and CDB Databases in PHP
call_user_func() or call_user_func_array() functions in PHP
Password must include both numeric and alphabetic characters - Magento
PHP code to import from CSV file to MySQL
PHP code to write to a CSV file for Microsoft Applications
Convert XML to CSV in PHP
do...while Loops in PHP
Using PEAR::Crypt_HMAC in PHP
Perl's Encoding::FixLatin equivalent in PHP
func_get_arg() and func_get_args() functions in PHP
break out of an if() block in PHP
Getting Started with PHP
isset() function in PHP