English 中文(简体)
使用__get、__set和__unset的类别中未预见的阵列内容
原标题:Unexpected array contents in class using __get, __set and __unset

我早就对Im的问题做了简短的演示。 这确实是我如何执行,但似乎会产生同样的结果。

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

......植被、__和__功能都在正确的地方被召唤,因此,我预计第二个功能是,直接要求该功能只是 un开 $本和gt,并用同样的数据再次填充。 计算这一变数时,会得出同样的产出。 相反,它最终完成上述工作......用三条直截了两条,并保持原先三套护法。

也许只是 mistake误或误解超负荷职能——要么如此,现在我就坐在这里太久了,因此,我们非常赞赏任何帮助!

最佳回答

你们的法典有许多错误:

  • 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.

更正本:

<?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;
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签