dongxia2030 2010-06-23 14:22
浏览 54
已采纳

使用__get,__ set和__unset在类中出现意外的数组内容

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!

  • 写回答

1条回答 默认 最新

  • dongyoulou4829 2010-06-23 15:30
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 目标跟踪,计算机视觉
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡
  • ¥20 基于MATLAB的TDOA
  • ¥15 为啥输入字体突然变了
  • ¥20 已知坐标,换成MATLAB可以用的数据