PHP
  Home arrow PHP arrow The Destruct Magic Function in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

The Destruct Magic Function in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2009-06-23


    Table of Contents:
  • The Destruct Magic Function in PHP 5
  • Review: the clone magic function
  • Working with destructors in PHP 5
  • Creating a persistable object with the destruct method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The Destruct Magic Function in PHP 5
    ( Page 1 of 4 )

    Welcome to the sixth part of a seven-part series that shows you how to use the magic functions that come with PHP 5. In this article, I cover the “__destruct()” method, also known as a destructor. They can be really useful for performing all sorts of clean-up tasks, or for creating objects that are capable of maintaining their state across several HTTP requests.

    To be frank, learning how to implement and use the magic functions that come included with PHP 5 isn’t as hard as it may seem at first glance. The entire process requires knowing which event triggers a particular function, and then giving that function a concrete definition in accordance with the specific needs of the application being developed.

    So, if you’re a PHP programmer who is searching for a comprehensive guide that introduces you gently to using the most important magic methods provided by PHP 5, then this group of articles may be the material that you need to start reading right away! 

    If you already went through all of the articles that precede this one, then it’s probable that by now you have a solid background in working with the most relevant PHP 5 magic functions. This includes, among others, the handy trio comprised of the “__set()”, “__get()” and “__call()” methods; the “__sleep()” and “__wakeup()” functions; and the “__clone()” method, which was explored in depth in the last tutorial of this series. 

    However, it’s fair to say that the aforementioned functions aren’t the only ones that you can use within your classes. There are a few others that can be really useful for performing relatively complex tasks with a minimal amount of code. That’s exactly the case with the “__destruct()” method, which will be called automatically by the PHP engine before destroying an instance of a particular class at the end of a script. 

    Indeed, implementing destructors is itself a huge subject that is out of the scope of this series for now. Yet, during this tutorial I’m going to discuss how to add a destructor to the sample “User” class that you saw in previous articles, so you can learn how to use it for constructing simple persistable objects. 

    Now, it’s time to get rid of the dull theory and start discovering how to use the “__destruct()” PHP 5 magic method. Let’s jump in!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT