PHP
  Home arrow PHP arrow Page 4 - Understanding Destructors 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

Understanding Destructors in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2008-01-16


    Table of Contents:
  • Understanding Destructors in PHP 5
  • Building a Sample
  • Introducing Class Destructors
  • Testing the Previous User

  • 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


    Understanding Destructors in PHP 5 - Testing the Previous User
    ( Page 4 of 4 )


    Seeing a destructor method in action: testing the previous user class

    If you’re anything like me, you may want to see how the prior “User” class functions after implementing its corresponding destructor. Well, I have to admit that I’m feeling quite curious too. Below I coded another sample script, which demonstrates how the destructor is called when the script finishes its execution, and consequently any object spawned from the previous user class is also destroyed.

    Now that I have explained that, please study the following code sample:


    try{

    // create new instance of 'User' class

    $user=new User('John','Doe','john@domain.com');

    // display separately user data

    echo 'First Name: '.$user->getFirstName().'<br />';

    echo 'Last Name: '.$user->getLastName().'<br />';

    echo 'Email: '.$user->getEmail().'<br />';

    // display all user information

    echo 'Complete user information: '.$user->getAll();

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    Frankly speaking, the above example looks identical to the one that you saw in the prior section. However, this first impression is a bit tricky; if you test this example on your system, you’ll see that it outputs the following on the browser:


    First Name: John

    Last Name: Doe

    Email: john@domain.com

    Complete user information: First Name: John Last Name: Doe Email Address: john@domain.com

    Destroying user now...

    Hey, what’s that last phrase at the end of the output? Yes, you guessed right! Once a user object is created and used in a simple manner, the script finishes running…but before this happens, the pertinent “__destruct()” method is called automatically, in this way displaying the message “Destroying user now…”

    At this moment, you’ll have to agree with me that working with destructors in PHP 5 is a very simple process. Naturally, my implementation of the “__destruct()” method in the previous example is primitive, but I believe that this should be clear enough for you to grasp the logic that stands behind using destructors.

    My final recommendation is simply that you play with this sample user class and test different implementations for its destructor to see what happens in each case.

    Final thoughts

    In this initial article of the series I walked you through learning the basic concepts that surround the use of class destructors with PHP 5. As you saw from the sample classes that I built previously, declaring and implementing these special class methods are actually no-brainer processes that can be grasped quickly, even if you’re only starting to work with classes and objects.

    In the next part of the series, things will become far more interesting and complex. I'm going to teach you how to work with multiple classes that implement obviously different destructors.

    Now that you’ve been warned, you simply can’t miss the next article!



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek