PHP
  Home arrow PHP arrow Page 3 - Overloading Classes 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

Overloading Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2006-07-25


    Table of Contents:
  • Overloading Classes in PHP 5
  • Overloading a property access in PHP 5: calling the “__set()” method
  • More about members overloading: triggering automatically the “__get()” method
  • Overloading multiple member accesses: combining the “__set()” and “__get()” method in the same class
  • Overloading a method call: triggering the “__call()” method

  • 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


    Overloading Classes in PHP 5 - More about members overloading: triggering automatically the “__get()” method
    ( Page 3 of 5 )

    As I stated before, triggering the “__get()” method behind the scenes via the overloading of a member access is actually a straightforward process. To demonstrate how this can be done, I’ll use the “DataSaver” class that you saw before, but this time I’ll replace its “__set()” method with a “__get()” method.

    Keeping in mind this small method replacement, the new definition of the “DataSaver” class is as follows:

    class DataSaver{
        private $data=array('Element1'=>1,'Element2'=>2,'Element3'=>3);
        private $dataFile='default_data_file.txt';
        // overload __get() method
        public function __get($index){
            echo 'Retrieving element of $data property with
    index='.$index;
            if(!$this->data[$index]){
                throw new Exception('The referenced element is not
    valid');
            }
            return $this->data[$index];
        }
        // save data to file
        public function save(){
            if(!$fp=fopen($this->dataFile,'a+')){
                throw new Exception('Error opening data file');
            }
            fwrite($fp,serialize($this->data));
            fclose($fp);
        }
        // fetch data from file
        public function open(){
            if(!$contents=file_get_contents($this->dataFile)){
                throw new Exception('Error reading from data file');
            }
            return unserialize($contents);
        }
    }

    As you’ll certainly agree, the above class isn’t rocket science at all. In simple terms, all that I did was replace the previous “__set()” method with a concrete definition of the new “__get()” method. Closely similar to the example you learned in the previous section, this method can be automatically called by coding the following script:

    // example of __get() overloading
    try{
        // instantiate 'DataSaver' object
        $dataSaver=new DataSaver();
        // change value of one element of $data property (calls the
    __get() method)
        echo 'The value of the following element property is
    '.$dataSaver->Element1;
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    In this specific case, the respective “__get()” method is automatically triggered when a class member access is overloaded by the line below:

    echo 'The value of the following element property is
    '.$dataSaver->Element1;

    Of course, after running the code contained inside the “__get()” method, this is the output that I get on my browser:

    Retrieving element of $data property with index=Element1
    The value of the following property is 1

    As you can see, the “__get()” method has been automatically called by simply overloading a class member access. Wasn’t that easy? You bet.

    Now that you learned how to trigger the corresponding “__set()” and “__get()” methods individually, the next step consists of demonstrating how these two methods can be integrated in the same class and called appropriately when overloading a couple of property accesses.

    To learn more on how this will be achieved, please go ahead and read the next section. 



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