PHP
  Home arrow PHP arrow Page 3 - 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? 
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 - Property overloading in action
    ( Page 3 of 4 )

    In the previous segment, I created a basic "User" class, whose API allowed us to easily assign and retrieve the values of its declared properties. However, as I expressed earlier, it's possible to shorten its  definition and make it more "dynamic" simply by concretely implementing the "__set()" and "__get()" methods.

    To demonstrate this concept a bit more, I'm going to redefine this sample class to allow us not only to create new class properties at run time, but retrieve their respective values with extreme ease.

    Now that you know what I'm going to do in the next few lines, please examine the modified version of the "User" class. It now looks like this:

    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;

    }

    }

    }

    Now, suddenly things are much more interesting than in the example shown in the previous section. As you can see above, the "User" class is only comprised of two methods -- the corresponding "__set()" and "__get()" functions. And in this specific situation, they have been given a concrete implementation.

    In the first case, the "__set()" function will create an undeclared property and assign a value to it, while in the last case, its counterpart "__get()" will retrieve this value if the property has been previously set.

    The implementation of these two methods permits us to overload properties in a truly simple way. It also makes the class's source code much shorter and more compact. But, as with everything in life, this process has some disadvantages that must be taken into account. First, and most importantly, the lack of an explicit declaration for each property of the class makes the code harder to read and follow. Second, it's practically impossible for an IDE to keep track of those properties for either commenting or documenting them.

    Logically, this is something that must be evaluated consciously when overloading the properties of a class. Its implementation depends strongly on the context where it will be used.

    Now, returning to the previous example class, given that it's already been modified, it's time to give it a try, right? So, with that idea in mind, in the next section I'm going to create an example that will show how to assign undeclared properties to it, and how to retrieve their values.

    Don't waste more time; read the following segment. It's only one click away



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT