PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - Creating a persistable object with the destruct method
    ( Page 4 of 4 )

     

    Undeniably, the best way to grasp how the previous “__destruct()” method is called by the PHP interpreter is by means of a concrete example. Thus, with that idea in mind, below I coded a simple script that spawns an instance of the “User” class, and then adds some properties to it. 

    The aforementioned script looks as follows:

     

    // example on implementing the '__destruct()' magic method

    $user = new User();

    $user->fname = 'Alejandro';

    $user->lname = 'Gervasio';

    $user->email = 'alejandro@mydomain.com';

     

    Admittedly, the above script looks pretty boring, aside from showing how to overload some properties of the “User” class. However, things are a bit tricky in this case, since right before the script finishes its execution (and the user object gets destroyed), the “__destruct()” method will be invoked, therefore storing the object on a session variable. 

    Of course, it’s also feasible to make the method save the object to a database table, or even to a flat file. Certainly, possibilities are numerous and very interesting to try out.  

    And finally, feel free to edit all of the code samples included in this tutorial, so you can arm yourself with a better background in using destructors in PHP 5.

    Final thoughts  

    Over this sixth episode of the series, I covered another magic method that comes packaged with PHP 5, that is the popular “__destruct(),” also known as a destructor. As was demonstrated previously, destructors 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. 

    Now, speaking specifically of the topics that will be discussed in the last tutorial, I must admit that there’s still a magic function that we haven't explored so far, which is used commonly within PHP programs to load classes automatically. 

    Yes, you guessed right -- I’m talking about the “__autoload()” function. If you want to learn how to work with it, then don’t miss the final chapter of this series!



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek