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

Overloading a property access in PHP 5: calling the “__set()” method - 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



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
    private $dataFile='default_data_file.txt';
    // define __set() method
    public function __set($index,$value){
        echo 'Assigning value '.'''.$value.'''. ' to element
with index='.$index;
            throw new Exception('The referenced element is not
    // 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);

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
    // 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();

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

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.

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