Home arrow PHP arrow Page 4 - Overloading Classes in PHP 5

Overloading multiple member accesses: combining the “__set()” and “__get()” method in the same class - PHP

Welcome to the last installment of the series “Overloading classes in PHP.” Comprised of three tutorials, this series shows you how to overload your classes whether you’re using PHP 4 or PHP 5, and explains in detail the implementation of the “__set()”, “__get()” and “__call()” methods, in order to overload property accesses and method calls respectively.

  1. Overloading Classes in PHP 5
  2. Overloading a property access in PHP 5: calling the “__set()” method
  3. More about members overloading: triggering automatically the “__get()” method
  4. Overloading multiple member accesses: combining the “__set()” and “__get()” method in the same class
  5. Overloading a method call: triggering the “__call()” method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 10
July 25, 2006

print this article



Assuming that you already understand the logic for triggering the “__set()” and “__get()” methods individually, combining them into the same class is just a matter of including the signature for each of them within the class you’re going to use. After adding these two methods, the definition of the “DataSaver” class looks like this:

class DataSaver{
    private $data=array('Element1'=>1,'Element2'=>2,'Element3'=>3);
    private $dataFile='default_data_file.txt';
    // define __set() method
    public function __set($index,$value){
        echo 'Assigning value '.'''.$value.'''. ' to element
with index='.$index.'<br />';
            throw new Exception('The referenced element is not
    // define __get() method
    public function __get($index){
        echo 'Retrieving element of $data property with
index='.$index.'<br />';
            throw new Exception('The referenced element is not
        return $this->data[$index];
    // save data to file
    public function save(){
            throw new Exception('Error opening data file');
    // fetch data from file
    public function open(){
            throw new Exception('Error reading from data file');
        return unserialize($contents);

At this point, the above class exposes the “__set()” and “__get()” method conjunctly, which comes in very handy for being triggered when overloading two property accesses. This process is illustrated below:

// example of __set() and __get() overloading
    // instantiate 'DataSaver' object
    $dataSaver=new DataSaver();
    // change one element of $data property (calls the __set()
    $dataSaver->Element2='This is element 2';
    // fetch element of $data property (calls the __get() method)
    echo 'The value of the following element property is
catch(Exception $e){
    echo $e->getMessage();

In this example, I overloaded two property accesses, in order to trigger in turn the “__set()” and “__get()” methods. I deliberately kept the code simple and readable, so you can see clearly how both methods are called in the script’s context, in addition to examining what type of output is generated by each of them.

All right, after executing the previous code snippet, the following messages are displayed sequentially:

Assigning value 'This is element 2' to element with
Retrieving element of $data property with index=Element1
The value of the following property is 1

Even when the example you saw before is rather trivial, it does demonstrate how two specific property accesses can be overloaded, resulting in the execution of the “__set()” and “__get()” methods respectively.

Do you think we’re done now? Not yet. I want to conclude this article showing you a simple example of how to overload a method call in PHP 5, which can be useful for triggering custom code wrapped into a “__call()” method. Therefore, go ahead and read the next few lines to see how this will be achieved.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: