Overloading Classes in PHP 5

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.

Introduction

As you learned in the previous articles, after a class has been overloaded by the “overload()” function that comes with PHP 4, it’s possible to run custom code automatically by calling the set of methods that I mentioned right at the beginning of this tutorial. Whether you’re overloading a property access, in order to call the “__set()” and “__get()” methods, or using an overloaded method call, which obviously triggers the “__call()” method, PHP provides you with the ability to invoke these methods automatically and run specific code without having to write explicit method calls inside an application.

Also, it should be noticed that overloading classes in PHP 4 isn’t as powerful as in other object-oriented languages like C++ or Python, but as I hopefully demonstrated by the numerous hands-on examples I showed previously, PHP offers a fairly comprehensive set of methods for overloading classes in a simple way.

Now that you learned the pros and cons of overloading classes in PHP 4, it’s time to move on and continue exploring the subject, since there are still a few additional topics that need to be covered. So, what’s comes next now? All right, in this last part of the series, I’ll be explaining how to overload classes specifically in PHP 5, which offers native support for overloading class members and methods, without having to explicitly call the PHP built-in “overload()” function.

Are you ready to learn how to overload your PHP 5 classes? Right, let’s begin.

{mospagebreak title=Overloading a property access in PHP 5: calling the “__set()” method}

Undoubtedly, the most significant difference between overloading classes in PHP 4 and PHP 5 rests on the native support offered by the last incarnation of PHP. True to form, PHP 5 will allow you overload your classes without having to use specifically the “overload()” function that was explained in previous articles.

In order to demonstrate how a property access can be overloaded in PHP 5, first I need to have a sample class to work with, so I’ll define one. In this case, the “DataSaver” class that I’ll use on different examples 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;
        if(!$this->data[$index]){
            throw new Exception(‘The referenced element is not
valid’);
        }
        $this->data[$index]=$value;
    }
    // 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 can see, the “DataSaver” class that I coded above looks really simple. What it does basically is save a serialized $data array to a text file and retrieve from it after reversing (unserializing) the process. However, aside from coding the corresponding “save()” and “open()” methods for performing these tasks, I provided a concrete implementation for a “__set()” method, which can be called automatically when a property access is overloaded. How is this done? Please take a look at the following example:

// example of __set() overloading
try{
    // instantiate ‘DataSaver’ object
    $dataSaver=new DataSaver();
    // change value of one element of $data property (calls the
__set() method)
    $dataSaver->Element1=’This is element 1′;
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

In this case, the example shown above first instantiates a new “DataSaver” object and then triggers the respective “__set()” method, by overloading a property access through the following line:

$dataSaver->Element1=’This is element 1′;

As you probably realized, the above sample class was overloaded without the need to use the “overload()” function, since PHP 5 comes with native support for overloading “__set()”, “__get()” and “__call()” methods. Additionally, PHP 5.1.0 also supports transparent overloading of the “isset()” and “unset()” functions via the “__isset()” and “__unset()” methods respectively, but since they won’t be covered in this article for the moment, you can learn more about this by reading the PHP manual.

With reference to the example shown before, after overloading a member access, the corresponding “__set()” method is called, resulting in the following output:

Assigning value ‘This is element 1′ to element with
index=Element1

The message listed above shows how the “__set()” method is successfully triggered when a property access is appropriately overloaded.

However, this tells only half of the story, since it’s also possible to call automatically a “__get()” method — provided that it was previously defined within the corresponding class — when a property access is overloaded. That’s precisely what I’ll teach you in the next section, therefore click on the link below and keep reading.

{mospagebreak title=More about members overloading: triggering automatically the “__get()” method}

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. 

{mospagebreak title=Overloading multiple member accesses: combining the “__set()” and “__get()” method in the same class}

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 />’;
        if(!$this->data[$index]){
            throw new Exception(‘The referenced element is not
valid’);
        }
        $this->data[$index]=$value;
    }
    // define __get() method
    public function __get($index){
        echo ‘Retrieving element of $data property with
index=’.$index.’<br />’;
        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);
    }
}

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
try{
    // instantiate ‘DataSaver’ object
    $dataSaver=new DataSaver();
    // change one element of $data property (calls the __set()
method)
    $dataSaver->Element2=’This is element 2′;
    // fetch 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 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
index=Element2
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.

{mospagebreak title=Overloading a method call: triggering the “__call()” method}

The last practical example that I’ll show you with reference to overloading classes in PHP 5 is completely focused on calling the “__call()” method automatically, when a method call is correctly overloaded. Maybe this sounds confusing, so first I’ll redefine the prior “DataSaver” class to provide a concrete definition for the “__call()” method that I mentioned before. Here’s the source code for this class:

class DataSaver{
    private $data=array(‘Element1′=>1,’Element2′=>2,’Element3′=>3);
    private $dataFile=’default_data_file.txt’;
    // define __call() method
    public function __call($method,$arguments){
        echo ‘Method ‘.$method. ‘ has been called with the
following arguments:<br />’;
        foreach($arguments as $argument){
            echo $argument.’<br />’;
        }
        return array_reverse($arguments);

    }
    // 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 shown above, the “DataSaver” class has an additional “__call()” method, which will be automatically triggered if a method call is overloaded deliberately. Given that, here is a simple script that shows how to overload a method call, which obviously fires up the method in question:

// example of method overloading with __call() method
try{
    // instantiate ‘DataSaver’ object
    $dataSaver=new DataSaver();
    // call inexistent ‘myMethod()’ method (invokes the __call()
method)
    $revData=$dataSaver->myMethod(‘Element A’,'Element
B’,'Element C’);
    echo ‘Reversed arguments are as follows:<br />’;
    foreach($revData as $data){
        echo $data.’<br />’;
    }
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

If you examine the above example  in detail, the corresponding “__call()” method is triggered by the following line:

$revData=$dataSaver->myMethod(‘Element A’,'Element B’,'Element
C’);

As you can see, all that this line does is call the “myMethod()” method, in this way enforcing the triggering of “__call()”. Also, it should be noticed that the pertinent arguments passed when overloading a method call will be treated as an array, therefore the output produced by the previous script will be the following:

Method myMethod has been called with the following arguments:
Element A
Element B
Element C
Reversed arguments are as follows:
Element C
Element B
Element A

The above listing clearly demonstrates that the  “__call()” method has been triggered after overloading a method call, since the array of incoming arguments is first echoed normally, then reversed and finally displayed again, in accordance with the logic implemented by this method.

At this point, I provided you with different practical examples of how to overload members and methods in PHP 5, which can be pretty useful if you want to run custom code defined within  “__set()”, “__get()” and “__call()” methods. As I said before, certainly class overloading isn’t one of the strongest features of PHP, but with a little bit of willpower and the appropriate knowledge, you’ll get the most out of it.

Wrapping up

Over this three-part series, you hopefully learned the basics of class overloading in PHP 4/PHP 5. In all the cases I kept the code samples simple and readable, so you can understand more easily how they work. Although overloading objects in PHP seems to be a rather complex topic at first glance, this impression should disappear progressively, if you get more experience on the subject.

As usual, see you in the next PHP tutorial!  

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan