PHP
  Home arrow PHP arrow Page 2 - Implementing Destructors with Multiple...
Administration  
AJAX  
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 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Implementing Destructors with Multiple Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2008-01-23

    Table of Contents:
  • Implementing Destructors with Multiple Objects in PHP 5
  • Handling user-related data with a simple PHP 5 class
  • Working with multiple objects by redefining the destructor of the previous “User” class
  • Finding out the order in which several destructors are called by the PHP interpreter

  • 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


    Implementing Destructors with Multiple Objects in PHP 5 - Handling user-related data with a simple PHP 5 class


    (Page 2 of 4 )

    Before I teach you how to work with multiple classes that provide concrete implementation for each of their destructors, it’s convenient to refresh a few fundamental concepts concerning the use of them at a very basic level. To do so, I’m going to use the sample class that I constructed in the last tutorial. As you'll recall, it came in handy for managing common user-related data, including first and last names and email addresses.

    Essentially, the signature of this user handling class looked like this:

    // basic example on using a '__destruct()' method

    // define 'User' class

    class User{

    private $firstName;

    private $lastName;

    private $email;

    public function __construct($firstName,$lastName,$email){

    if(!$firstName||strlen($firstName)>32){

    throw new Exception('Invalid First Name parameter!');

    }

    if(!$lastName||strlen($lastName)>32){

    throw new Exception('Invalid Last Name parameter!');

    }

    if(!$email||!preg_match("/^.+@.+..+$/",$email)){

    throw new Exception('Invalid Email parameter!');

    }

    $this->firstName=$firstName;

    $this->lastName=$lastName;

    $this->email=$email;

    }

    // get user's first name

    public function getFirstName(){

    return $this->firstName;

    }

    // get user's last name

    public function getLastName(){

    return $this->lastName;

    }

    // get user's email

    public function getEmail(){

    return $this->email;

    }

    // get all user data

    public function getAll(){

    return 'First Name: '.$this->firstName.' Last Name: '.$this->lastName.' Email Address: '.$this->email;

    }

    // implement a __destruct()' method

    public function __destruct(){

    echo '<h2>Destroying user now...<h2>';

    }

    }


    Frankly speaking, I must say that the definition of the above “User” class is extremely easy to understand. So you shouldn’t have much of a problem grasping how it functions. However, I’d like you to pay close attention to the “__destruct()” method declared in the end. In this case, as with all of the magic methods provided by PHP 5, it’s preceded by two underscore (__) characters and indicates that the interpreter is going to execute it right before destroying an object created with this class.

    This concept may sound rather confusing if it’s not accompanied by a practical example. Therefore, below I coded a simple script that spawns a new user object, displays its properties' values on the browser, and then, as you’d expect, triggers the corresponding destructor prior to finishing its execution.

    Having explained that, please take a look at 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();

     

    /* displays the following

    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...

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    There you have it. Check the above example with your own testing web server and you’ll be amazed at how well it works! Perhaps it’s not going to change your life as a PHP programmer forever, but undoubtedly it’s going to show you how a destructor functions. In this case, the implementation of the destructor is rather primitive, yet this example should fire up your creativity so that you use your own class destructors in a more useful way.

    Well, at this moment you’ve hopefully recalled the foundations of working with destructors in PHP 5. So assuming that this is true, the next thing that I’m going to teach you will be how to use multiple objects that concretely implement their respective destructors. In doing this, you’ll be able to see very clearly in what order these methods are called by the PHP interpreter right before the objects are destroyed.

    To find out how this will be done, please visit the following section and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · In the first episode of the series, I introduced you to using destructors in PHP 5,...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT