PHP
  Home arrow PHP arrow Page 3 - 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 - Determining what properties to create within a class
    ( Page 3 of 4 )

    In accordance with the concepts deployed in the preceding section, I’m going to change the implementation of the “__set()” method defined within the “User” class that we learned before, so it can restrict the creation of undeclared properties only to “fname,” “lname” and “email.” If a script tries to create a property other than the ones specified, the process will simply fail gracefully.

    Now that I've explained the modifications that I plan to introduce to the “User” class, please pay attention to its brand new signature, which is as follows:

    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;

    }

    }

    }

    See how easy it is to give the above “User” class the ability to control which properties to create and which to refrain from creating? I guess you do! As I explained before, in this particular example, the class will only allow the creation of the “fname,” “lname” and “email” properties, so any attempt to create a different one will be unsuccessful.

    In order to demonstrate more clearly how this example class now works, below I coded a script that illustrates what happens when a “forbidden” property is added to an instance of the “User” class. Here it is:

    // example on using 'User' class with property overloading

    $user = new User();

    $user->fname = 'Alejandro';

    $user->lname = 'Gervasio';

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

    // this property won't be created

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

    */

    That was pretty interesting, right? As you can see above, the line that attempts to create a new property called “address” will fail because of the control implemented within the “__set()” method.

    While rather simplistic, this example should give you a clearer idea of how powerful these magic functions can be when it comes to simplifying the creation and assignment of properties to a given class.

    Well, having finished this brief introduction to using the “__set()” and “__get()” functions, it’s time to continue exploring other magic methods bundled with PHP 5. So, with that idea in mind, in the last segment of this article I’m going to discuss the use of the complementary “__isset()” and “__unset()” functions.

    To learn more about these functions, please go ahead and read the following section. It’s only one click away.



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