Page 1 of 1

Overloading Inheritence

Posted: Sat Feb 09, 2008 4:20 pm
by dayyanb
I want to use __set to set a variable in a parent class.

Example:

Code: Select all

 
<?php
    class test {
        public $var;
        public function __set($var, $val) {
            $this->$var = $val . ' and was set by parent';
        }
    }
    class testtwo extends test {
        public $var2;
        public function __set($var, $val) {
            parent::__set($var, $val);
        }
    }
    $testtwo = new testtwo;
    $testtwo->var = 'hello';
    $testtwo->var2 = 'hello';
    echo('var is ' . $testtwo->var . '<br />');
    echo('var2 is ' . $testtwo->var2);
?>
 
returns:

Code: Select all

var is hello
var2 is hello
How could I make it return

Code: Select all

var is hello and was set by parent
var2 is hello and was set by parent

Re: Overloading Inheritence

Posted: Sat Feb 09, 2008 4:27 pm
by Christopher
Please read the manual section about __set().

Code: Select all

   class test {
        var $_data = array();
        public function __set($var, $val) {
            $this->_data[$var] = $val . ' and was set by parent';
        }
        public function __get($var) {
            return $this->_data[$var];
        }
    }
    class testtwo extends test {
    }
    $testtwo = new testtwo;
    $testtwo->var = 'hello';
    $testtwo->var2 = 'hello';
    echo('var is ' . $testtwo->var . '<br />');
    echo('var2 is ' . $testtwo->var2);

Re: Overloading Inheritence

Posted: Sat Feb 09, 2008 4:41 pm
by dayyanb
I had read http://us.php.net/manual/en/language.oo ... oading.php but I didn't realize it had to be an array. Why doesn't it work for a normal variable?

Re: Overloading Inheritence

Posted: Sat Feb 09, 2008 4:58 pm
by Christopher
dayyanb wrote:Why doesn't it work for a normal variable?
Because __get() and __set() are really error handlers that are only called if a property is referenced that does not exist. If you create the property then any logic you have in __get()/__set() will not be called on second use of the property -- because it will then exist.

Re: Overloading Inheritence

Posted: Sat Feb 09, 2008 6:23 pm
by dayyanb
Oh I completely misunderstood their use then.

I thought I should use them because of a post in another thread:
If you're using PHP5, don't create a new function setVar(), overwrite the magic method __set() (which essentially does the same thing, but invoking it is cleaner).
viewtopic.php?f=50&t=78105

Re: Overloading Inheritence

Posted: Sat Feb 09, 2008 6:33 pm
by Christopher
Yep. But don't forget that there are very good reasons for setters. The allow controlled access to variables. For example:

Code: Select all

// only allow percents 0 to 100
function setPercent($percent) {
    if (($percent >= 0) && ($percent <= 100)) {
        $this->percent = $percent;
    }
}

Re: Overloading Inheritence

Posted: Sat Feb 09, 2008 7:06 pm
by dayyanb
arborint wrote:Yep. But don't forget that there are very good reasons for setters. The allow controlled access to variables.
Ok, thank you for your time. I am going to use normal set functions instead of Overloads. This situation seems rather hackish. I am actually creating a class to change information about a user. I am going to get the list of allowed variables directly from the columns in the mysql table and this would create problems if the column name was the same as a name of a variable in my class.