Hudzilla.org - the homepage of Paul Hudson
Contents > Objects > Magic functions Wish List | Report Bug | About Me ]

6.14.3     __set()

This is NOT the latest copy of this book; click here for the latest version.

The __set() magic function complements __get(), in that it is called whenever an undefined class variable is set in your scripts. This is a little harder to use with good reason, however, and is more likely to confuse than help.

Here is one example of how you could use __set() to create a very simple database table class and perform ad hoc queries as if they were members of the class:

<?php
    
//...[snip - add your MySQL connection code here]...

    
class mytable {
        public
$Name;

        public function
__construct($Name) {
            
$this->Name = $Name;
        }

        public function
__set($var, $val) {
            
mysql_query("UPDATE {$this->Name} SET $var = '$val';");
        }

        
// public $AdminEmail = 'foo@bar.com';
    
}

    
$systemvars = new mytable("systemvars");
    
$systemvars->AdminEmail = 'telrev@somesite.net';
?>

In that script $AdminEmail is commented out, and therefore does not exist in the mytable class. As a result, when $AdminEmail is set on the last line, __set() is called, with the name of the variable being set and the value it is being set to being passed in as parameters one and two respectively. This is used to construct an SQL query in conjunction with the table name passed in through the constructor. While this might seem like an odd way to solve the problem of setting key database values, it is pretty hard to deny that the last line of code ($systemvars->AdminEmail...) is actually very easy to read.

This system could be extended to more complicated objects as long as each object knows their own ID number.

Author's Note: By default, PHP lets you set arbitrary values in objects, even if their classes don't have that value defined. If this annoys you (if, for example, you used OPTION EXPLICIT in your old Visual Basic scripts) you can simulate the behaviour by using __get() and __set() to print errors.





<< 6.14.2 __get()   6.14.4 __call() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is two plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow