PHP
  Home arrow PHP arrow Page 4 - 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

Magic Functions in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2009-05-26


    Table of Contents:
  • Magic Functions in PHP 5
  • Get and set functions: a quick overview
  • Property overloading in action
  • The set and get methods in action

  • 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


    Magic Functions in PHP 5 - The set and get methods in action
    ( Page 4 of 4 )

    In the previous segment, you learned how to give a concrete implementation to the "__set()" and "__get()" magic functions within a simple class to assign to it undeclared properties at run time. You also learned how to fetch values in a straightforward manner.

    Now, it's time to demonstrate how to take advantage of these functions by means of a concrete example. Below I wrote a short script, which shows how to dynamically assign four properties to the "User" class that naturally have no explicit declaration.

    Here's how this script looks:

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

    */

    Regardless of the simplicity of the above example, it demonstrates how useful the "__set()" and "__get()" magic functions can be when it comes to overloading properties of a class.

    As you may have noticed, on one hand the script creates four new properties for the class and assigns to them some trivial values, while on the other hand those values are retrieved "behind the scenes" via the pertinent "__get()" method. Simple to code, even when it's not so simple to read, right?

    The previous phrase is an adequate epilogue for this first installment of the series. As I explained before, the implementation of the "__set()" and "__get()" magic functions is a powerful approach that must be used very carefully.

    Undeniably, it's possible to perform all sorts of clever tasks via these functions, but this versatility comes at a cost. In general, it may be harder to read and understand how a class is handling its properties. Anyway, PHP 5 gives you the chance to use property overloading in an easy way, so go ahead and give it a try!

    Final thoughts

    That's all for the moment. In this introductory part of the series, I explained how to implement and use the "__set()" and "__get()" magic functions included with PHP 5, which allow you to overload the properties of a class very easily.

    It's not new to say that property overloading is a powerful approach that can be used to perform all sorts of clever tasks, but as I explained, it must be used carefully, and certainly not abused.

    In the next part of the series I'm going to explain how to use the "__isset()" and "__unset()" magic functions, so don't miss the upcoming tutorial!



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

       

    PHP ARTICLES

    - 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...
    - Mastering WHILE Loops for PHP and MySQL





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