簡體   English   中英

__isset中的未定義索引

[英]Undefined Index in __isset

我在__isset中的實際代碼中收到錯誤,但是當我轉到“運行php在線網站”時,它可以工作。 所以我不確定為什么它在我的代碼中不起作用。

<?
class Test {

    private $args;

    public function __construct($args = array()) {
        $this->args = $args;
    }

    public function __isset($name) {
        return $this->args[$name]; //undefined index on my server (is fine on php site)
    }

    function prnt() {
        // echo isset($this->i) ? "set" : "notset"; --> works, both print 'set'
        echo isset($this->h) ? "set" : "notset";
    }

}
?>

然后執行以下命令:

$test = new Test(array('i' => '1234'));
$test->prnt();
//result on php website: notset
//result on my website: Undefined index at the line shown above.

可能有用的信息:
我的服務器正在運行php 5.1。
isset($this->var)在我的實際代碼中的include文件中發生。
只要變量存在(就像上面的i一樣),它顯然就可以工作。

您正在嘗試返回不存在的鍵的值,而是使用array_key_exists返回鍵的測試結果

public function __isset($name) {
    return array_key_exists($name, $this->args);
}

您在每種環境中的錯誤報告設置都不同。 一種環境允許E_NOTICE級別的錯誤通過,而另一種則阻止它們。

您應該執行以下操作:

return array_key_exists($name, $this->args);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM