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

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


    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
     

       

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