views:

26

answers:

1

I've created a short demonstration of the problem I'm having. This isn't exactly how I'm implementing it but seems to lead to the same result.

<?php

class mainclass {

    var $vardata = array();

    function &__get ($var) {
        if ($this->vardata[$var]) return $this->vardata[$var];
        if ($var == 'foo') return $this->_loadFoo();
        return NULL;
    }

    function __set ($var, $val) {
        $this->vardata[$var] = $val;
    }

    function __unset($var) {
        unset($this->vardata[$var]);
    }

}

class extender extends mainclass {

    function __construct() {

        var_dump($this->foo);
        $this->_loadFoo();
        echo '<br>';
        var_dump($this->foo);

    }

    function _loadFoo () {

        unset($this->foo);
        $this->foo = array();

        $this->foo[] = 'apples';
        $this->foo[] = 'oranges';
        $this->foo[] = 'pears';

        return $this->foo;

    }

}


$test = new extender;

?>

The output of the above code is:

array(3) { [0]=>  string(6) "apples" [1]=>  string(7) "oranges" [2]=>  string(5) "pears" }
array(5) { [0]=> string(6) "apples" [1]=> string(7) "oranges" [2]=> string(5) "pears" [3]=> string(7) "oranges" [4]=> string(5) "pears" } 

Where as I was expecting:

array(3) { [0]=>  string(6) "apples" [1]=>  string(7) "oranges" [2]=>  string(5) "pears" }
array(3) { [0]=>  string(6) "apples" [1]=>  string(7) "oranges" [2]=>  string(5) "pears" }

The __get, __set and __unset functions are all being called in the right places and so I'd have expected the second, direct calling of the function to simply unset $this->foo and fill it again with the same data. Meaning that var_dumping it would give the same output. Instead, well, it ends up doing the above... filling it with two of the three strings directly set and keeping the first three originally set strings.

Perhaps just a silly mistake or a misunderstanding of the overloading functions - either way, I've been stuck here for too long now and so any help is much appreciated!

A: 

There were many things wrong with your code:

  • if ($this->vardata[$var]) is not the way to check is a variable exists; isset or (if null's are allowed, array_key_exists) is.
  • The mainclass called a function it doesn't have. You should have declared it abstract (it would still work otherwise, but you'd be exposing yourself to bugs if you instantiated the mainclass).
  • You didn't make your mind if you wanted _loadFoo to set a missing array element (work with collateral effects) or return the default value for the foo property. Since you're returning by reference in __get, I suppose you want to have an actual variable and not just a return value, so I went for the first route. You had _loadFoo doing both things – set the missing array index and returning the value; to complicate things you did not set the array index directly, you relied on overloads.

Corrected version:

<?php

abstract class mainclass {

    var $vardata = array();

    function &__get ($var) {
        if (isset($this->vardata[$var])) return $this->vardata[$var];
        if ($var == 'foo') {
            $this->_loadFoo(); /* called for collaterals */
            return $this->foo;
        }
        return NULL;
    }

    abstract function _loadFoo();

    function __set ($var, $val) {
        $this->vardata[$var] = $val;
    }

    function __unset($var) {
        unset($this->vardata[$var]);
    }

}

class extender extends mainclass {

    function __construct() {

        var_dump($this->foo);
        $this->_loadFoo();
        echo '<br>';
        var_dump($this->foo);

    }

    function _loadFoo () {

        unset($this->foo);
        $this->foo = array();

        $this->foo[] = 'apples';
        $this->foo[] = 'oranges';
        $this->foo[] = 'pears';

    }

}

$test = new extender;
Artefacto
Thanks. Adding the isset sorts it in my implementation, bit of an oversight there. Other issues pointed out are relevant points about the sample code I posted - thanks for taking the time to look through it.
JoeR