使用 __get、__set 和 __unset 时类中出现意外的数组内容

发布于 2024-09-06 10:18:11 字数 1726 浏览 12 评论 0原文

我已经为我遇到的问题创建了一个简短的演示。这并不完全是我的实现方式,但似乎会产生相同的结果。

<?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;

?>

上面代码的输出是:

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" } 

正如我所期望的:

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" }

__get、__set 和 __unset 函数都在正确的位置被调用,所以我期望第二次直接调用该函数来简单地取消设置 $this- >foo 并用相同的数据再次填充它。这意味着 var_dumping 会给出相同的输出。相反,它最终会执行上述操作...用直接设置的三个字符串中的两个填充它,并保留前三个最初设置的字符串。

也许只是一个愚蠢的错误或对重载函数的误解 - 不管怎样,我已经被困在这里太久了,所以非常感谢任何帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌吟 2024-09-13 10:18:11

您的代码有很多问题:

  • if ($this->vardata[$var]) 不是检查变量是否存在的方法; isset 或(如果允许 null,array_key_exists)是。
  • mainclass 调用了它没有的函数。您应该将其声明为抽象的(否则它仍然可以工作,但是如果您实例化主类,您将使自己面临错误)。
  • 您没有决定是否希望 _loadFoo 设置缺失的数组元素(与附带效果一起使用)或返回 foo 属性的默认值。由于您在 __get 中通过引用返回,我想您想要一个实际的变量而不仅仅是一个返回值,所以我选择了第一条路线。您让 _loadFoo 完成这两件事 – 设置缺失的数组索引并返回值;让事情变得复杂的是,您没有直接设置数组索引,而是依赖于重载。

更正版本:

<?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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文