PHP
  Home arrow PHP arrow Page 2 - 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 - Review: the set and get magic functions
    ( Page 2 of 4 )

    Before I start explaining how to use the “__isset()” and “__unset()” magic functions, it would be helpful to reintroduce the example developed in the preceding tutorial. It demonstrated how to use the “__set()” and “__get()” methods for overloading a basic user-related class.

    Having said that, here’s the complete source code corresponding to that sample class:

    class User

    {

    // constructor (not implemented)

    public function _construct(){}

     

    // set undeclared property

    function __set($property, $value)

    {

    $this->$property = $value;

    }

     

    // get defined property

    function __get($property)

    {

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

    {

    return $this->$property;

    }

    }

    }

    I’m not going to explain again how the above “User” class does its business, since this was already covered in depth in the previous tutorial. Instead, I’m going create a short script that shows how to dynamically add some properties to the class in question, and to retrieve their respective values.

    So, basically the script that performs all of these interesting tasks looks like this:

    // example of usage of 'User' class with property overloading

    $user = new User();

    $user->fname = 'Alejandro';

    $user->lname = 'Gervasio';

    $user->email = 'alejandro@mydomain.com';

    $user->address = 'My address 1234';

     

    // display user data

    echo 'First Name: ' . $user->fname . ' Last Name: ' . $user->lname . ' Email: ' . $user->email . ' Address: ' . $user->address;

    /*

    displays the following

    First Name: Alejandro Last Name: Gervasio Email: alejandro@mydomain.com Address: My address 1234

    */

    There you have it. By giving a concrete implementation to the “__set()” and “__get()” functions, it is possible to add new properties to the “User” class without having to declare them explicitly. Of course, in doing so the whole source code of the class might become less readable. Unfortunately, that’s the price that must be to paid when it comes to overloading class properties.

    So far, so good. At this stage you've hopefully become familiar with using the handy “__set()” and “__get()” magic functions. Given the inherent flexibility these function provide, it’s also feasible to modify their implementation and give them the capacity to determine which properties of a class to create and which to avoid creating, by means of a simple control mechanism.

    Assuming that you’re interested in learning how to build this checking mechanism, in the section to come I’m going to change the definition of the previous “User” class so it can restrict the creation of undeclared properties according to a predefined schema.

    To see how this will be accomplished, click on the link below and keep reading.



     
     
    >>> 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