PHP
  Home arrow PHP arrow Page 2 - Executing Destructors Manually in PHP ...
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 
IBM Developerworks
 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

Executing Destructors Manually in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-02-13

    Table of Contents:
  • Executing Destructors Manually in PHP 5
  • Triggering a destructor manually
  • Calling multiple destructors manually
  • Emulating destructors in PHP 4

  • 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

    Lose your application development headaches. Start developing and deploying applications with Advantage Database Server today. Download a 30-day trial for Free!

    Executing Destructors Manually in PHP 5 - Triggering a destructor manually
    (Page 2 of 4 )

    As I said in the beginning, it’s perfectly possible to call the destructor of a determined object without having to end the execution of an application or script. Since in all the cases a destructor will be invoked when its pertinent object no longer exists, this process can be triggered manually simply by removing the  object via the “unset()” PHP native function.

    As you’ll possibly know, this function comes in handy for removing unwanted variables of all types, and these also include objects. The concept is theoretically interesting, therefore I’m going to demonstrate how to manually call a destructor by using the same “User” class that you saw in previous articles of the series.

    Its respective definition was the following:


    // 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(){

    // display object properties

    echo '<h2>Properties of object being destroyed</h2>';

    foreach(get_object_vars($this) as $prop=>$val) {

     echo '<p>'.$prop.'='.$val.'</p>';

     }

    // display object methods

    echo '<h2>Methods of object being destroyed</h2>';

    $methods=get_class_methods(get_class($this));

    foreach($methods as $method) {

    echo '<p> Method Name: '.$method.'()</p>';

    }

    }

    }


    Now that you hopefully recalled how the above class does its business, let me show you a concrete example where its respective constructor is called manually using the popular “unset()” PHP function.

    Having said that, here’s how this brand new example looks:


    try{

    // create user

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

    // display separately user data

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

    // delete object, so this triggers its '__destruct()' method

    unset($user);

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

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

    // display all user information

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

     

    /* displays the following:

     

    First Name: John

     

    Properties of object being destroyed

     

    firstName=John

    lastName=Doe

    email=john@domain.com

     

    Methods of object being destroyed

     

    Method Name: __construct()

    Method Name: getFirstName()

    Method Name: getLastName()

    Method Name: getEmail()

    Method Name: getAll()

    Method Name: __destruct()


    Fatal error: Call to a member function getLastName() on a non-object in path/to/example

     

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    After studying the previous example, you’ll have to admit that things suddenly become exciting! As you can see, once a generic user handing object is created by the above script, the object in question is deliberately deleted by way of the aforementioned “unset()” PHP function.

    This trick results in the immediate execution of its destructor, certainly a process shown clearly by the output generated by the prior script. Now, at this point, do  you see how easy it is to make a destructor run without waiting for a script to finish? I guess you do!

    However, the example that you just learned uses only one object along with its corresponding destructor. But what about using multiple objects and destructors that are called manually? Indeed, it sounds like something promising and useful. Thus in the next section, I’m going to show you precisely how to fire up different destructors by using the “unset()” PHP function utilized previously.

    To learn the details of how this process will be achieved, please click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · Over the course of this final episode of the series, you’ll learn you how to trigger...
     

       

    PHP ARTICLES

    - 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
    - Working with Multiple Document Nodes with th...




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