PHP
  Home arrow PHP arrow Page 3 - The Destruct Magic Function 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

The Destruct Magic Function in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2009-06-23


    Table of Contents:
  • The Destruct Magic Function in PHP 5
  • Review: the clone magic function
  • Working with destructors in PHP 5
  • Creating a persistable object with the destruct method

  • 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


    The Destruct Magic Function in PHP 5 - Working with destructors in PHP 5
    ( Page 3 of 4 )

     

    As I stated in the previous segment, it’s possible to accomplish interesting and useful things with a destructor. In this case, I’m going to use it within the already familiar “User” class to create an object that will save itself to a session variable before being destroyed. 

    How will this be achieved? Well, take a look at the enhanced definition of the “User” class, which now implements the “__destruct()” method. Here it is:

     

    // define 'User' class (implements the __destruct() magic method)

    class User

    {

    // constructor (not implemented)

    public function _construct(){}

     

    // set undeclared property in a restrictive way

    public function __set($property, $value)

    {

    if (in_array($property, array('fname', 'lname', 'email')) === TRUE)

    {

    $this->$property = $value;

    }

    }

     

    // get declared property

    public function __get($property)

    {

    if (isset($this->$property))

    {

    return $this->$property;

    }

    }

     

    // single point to fetch user data

    public function __call($method, $args)

    {

    if ($method === 'fetch' AND empty($args) === FALSE)

    {

    return $this->$args[0];

    }

    }

    public function __destruct()

    {

    session_start();

    $_SESSION['user'] = serialize($this);

    }

    }

     

    As shown above, the implementation of the “__destruct()” method is very easy to follow. All it does is start or resume a new session, and then save an instance of the “User” class to a session variable. Period. 

    Logically, nothing spectacular is happening here, except for the fact that all of these tasks will be performed before the instance of the user class gets destroyed. In this manner we're building an object that has the ability to maintain its state across different HTTP requests, by using only the PHP built-in session mechanism. 

    Even though the definition of the “__destruct()” method is indeed self-explanatory, an example is in order her to understand how it works. Therefore, in the last section of this tutorial I’m going to set up such an example, so you can see how the destructor does its thing. 

    To learn how this wrapping example will be created, click on the link shown below and keep reading. We’re almost done exploring destructors!

     



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