PHP
  Home arrow PHP arrow Page 4 - Understanding Destructors in PHP 5
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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

Understanding Destructors in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · The improved object model of PHP 5 permits to declare destructors. Basically, a...
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway