PHP
  Home arrow PHP arrow Page 2 - Implementing Destructors with Multiple Objects 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

Implementing Destructors with Multiple Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    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 2 Hosted by Hostway
    Stay green...Green IT