PHP
  Home arrow PHP arrow Page 2 - The Call Magic Function 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 Call Magic Function in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2009-06-08


    Table of Contents:
  • The Call Magic Function in PHP 5
  • Review: the isset and unset magic functions
  • Method overloading in PHP 5 with the call magic function
  • The call magic method 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


    The Call Magic Function in PHP 5 - Review: the isset and unset magic functions
    ( Page 2 of 4 )

    It's quite possible that you haven't had the chance to read the preceding tutorial of this series, where I discussed briefly how to implement and use the "__isset()" and "__unset()" magic functions.

    With this in mind, I've included the example created in that article, which shows how to utilize those functions within a basic class called "User."

    Here's the complete source code that corresponds to the aforementioned "User" class. Have a look at it, please:

    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;

    }

    }

    Now that I have listed the entire signature of the above "User" class, which shows a simple implementation of the "__isset()" and "__unset()" magic methods, please focus your attention on the following code snippet. It demonstrates how they behave when using the "isset()" and "unset()" PHP functions with an instance of the previous user-related class.

    Here's the corresponding code sample:

    $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

    */

    As you can see, using the "__isset()" and "__unset()" functions isn't too different from working with other magic methods, such as "__set()" and "__get()" that were discussed in the first tutorial. All that's required here is to give an explicit implementation to the functions and then use "isset()" and "unset()" to make the PHP engine call them automatically. It's that simple, really.

    Well, at this point I'm sure that you've already grasped the underlying logic of the previous example. Thus, it's time to continue exploring other PHP 5 magic functions, and that logically includes the "__call()" method mentioned in the introduction.

    With that idea in mind, in the following section I'm going to explain how to work with this brand new method to implement what is widely called "method overloading" in object-oriented jargon.

    Now, to learn more on this topic, click on the link that appears below and proceed to read the section to come. I'll be there, waiting for you.



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