PHP
  Home arrow PHP arrow 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
    ( Page 1 of 4 )

    Welcome to the second part of a seven-part series on the magic functions in PHP 5. In the previous article, we looked at property overloading with the get and set functions. In this one, we'll take a look at the same task using the isset and unset magic functions.

    As you may have heard, PHP 5 comes packaged with a set of unimplemented functions that are popularly known as “magic functions” or “magic methods.” These are called internally by the PHP engine when performing a number of tasks, such as including classes that haven’t been found at runtime, when attempting to assign values to undeclared properties of a class, or when a script tries to call a non-existent method, only to cite a few examples.

    Also, magic functions will be invoked transparently by the PHP parser when serializing and unserializing objects, as well as when creating and destroying instances of a class. This allows us to execute additional processes and trigger predefined actions behind scenes in an easy way.

    Obviously, giving a concrete implementation to this arsenal of functions permits us to do to all sorts of clever things with a minimal amount of code. Please remember that this may eventually be detrimental to the readability and maintenance of an application.

    Despite these issues, there are times when the implementation of particular magic functions, like “__set()” and “__get()” for instance, comes in handy for easily overloading properties of a class. And now that I've brought the subject to the table, in the first chapter of this series, I used a variety of code samples to demonstrate how to overload properties of a sample class called “User” by using the complementary “__set()” and “__get()” magic functions.

    Those examples showed how to dynamically create undeclared properties of the aforementioned class; they also demonstrated how to retrieve their values, either for display purposes or for further processing.

    However, overloading properties is only one of the many useful things that can be accomplished with magic functions. It’s also possible to perform additional tasks when working with the “isset()” and “unset()” PHP functions, but in this case by using another set of complementary functions, called “__isset()" and “__unset()” respectively.

    So, in the next few lines I’m going to delve more deeply into the advantages of using property overloading, and then take a close look at the team composed of the “__isset() “ and “__unset()” magic functions, so you can quick grasp how to use them in concrete situations.

    Now, let’s get rid of the rather boring theory and continue discovering the real power behind working with magic functions in PHP 5. Let’s go!



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