PHP
  Home arrow PHP arrow Page 4 - The Isset and Unset Magic Functions 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

The Isset and Unset Magic Functions in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-06-02


    Table of Contents:
  • The Isset and Unset Magic Functions in PHP 5
  • Review: the set and get magic functions
  • Determining what properties to create within a class
  • Introducing the isset and unset magic functions

  • 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 Isset and Unset Magic Functions in PHP 5 - Introducing the isset and unset magic functions
    ( Page 4 of 4 )

    As their names clearly suggest, the “__isset()” and “__unset()” magic functions are automatically called by the PHP engine when the popular “isset()” and “unset()” functions are used in that sequence within a script.

    As with all of the other magic functions, these have no concrete implementation, which allows us to create “hooks” very easily when unsetting and checking the existence of a certain PHP variable.

    But surely you've learned all of this theory over and over again each time you looked at the PHP manual, so let’s proceed to code a simple example that shows the “__isset()” and “__unset()” functions in action.

    Remember the “User” class that I utilized in the previous segment? Good. Based on its definition, I’m going to give an explicit implementation to the aforementioned functions, to show you how they can be used in a concrete case.

    Below is the class with the appended “__isset()” and “__unset()” functions:

    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)

    {

    return $this->$property;

    }

     

    // implement __isset() method

    public function __isset($property)

    {

    echo 'Checking if property '. $property . ' has been set...';

    }

     

    // implement __unset() method

    public function __unset($property)

    {

    echo 'Unsetting property ' . $property;

    }

    }

    As shown above, the implementation of the “__isset()” and “__unset()” methods is admittedly very trivial but hopefully illustrative. In the first case, if the “isset()” function is called with an instance of the “User” class, then a brief message will be displayed on screen, informing of the occurrence of this process.

    On the other hand, if “unset()” is used with a user object, then the complementary “__unset()” method will be called automatically as well.

    Want to see how this description can be translated into functional code? Well, take a look at the following code sample to dissipate all of your doubts:

    $user = new User();

    $user->fname = 'Alejandro';

    $user->lname = 'Gervasio';

    isset($user->email);

    /*

    display the following

    Checking if property email has been set...

    */

     

    unset($user->email);

    /*

    displays the following

    Unsetting property email

    */

    Hopefully, the above example demonstrates in a clear fashion the behavior of the “__isset()” and “__unset()” methods. Of course, this is only an example, which means that it’s feasible to give more complex implementations to the methods in question to make them perform more useful tasks. But, that will be left as homework for you, so you can build a killer script that makes these functions really shine!

    Final thoughts

    That’s about it for now. In this second tutorial of the series I demonstrated how to extend the use of the “__set()” and “__get()” magic functions to build a simple mechanism that controls which properties of a class must be created. I also explained a trivial use of the “__isset()” and “__unset()” functions, which as you saw previously, are actually pretty simple to understand.

    In the upcoming article, I’m going to discuss how to overload methods of a class via the “__call()” magic function. Now that you’ve been told the topic that will be covered in that tutorial, you can miss it!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek