2010-06-23 35 views
0

我有一個類的方法__toString和__get和一些受保護的屬性,如「消息」。到現在爲止還挺好。PHP分段錯誤| __get在類內部調用受保護的屬性?

現在的問題是,我需要訪問__toString中的$ this->消息,並且當$ display_all被設置爲true時,這會導致(並非總是失敗)分段錯誤。你知道爲什麼以及如何解決它嗎?

非常感謝! 羅爾夫

PS:這裏是一個例子

class FuckedClass { 
    protected $file; 
    protected $line; 
    protected $display_all; 
    protected $message; 

    //[...] 

    /** 
    * Magic getter 
    * @param String $name 
    * @return mixed 
    */ 
    public function __get($name) { 
     return (in_array($name,array_keys(get_class_vars(__CLASS__))))? 
        $this->$name : null; 
    } 
    /** 
    * Formats 
    */ 
    public function __toString() { 
     $message = $this->message . ($this->display_all) ? 
       '[ Location: '.$this->file.' @ line '.$this->line.' ]': 
       ''; 
     $text =<<<PLAIN 
Error : {$message} 
PLAIN; 
     return $text; 
    } 
} 

//instantiated $fucked_class 
die($fucked_class);  
+0

你可以發佈一個簡短的腳本來舉例說明問題嗎? – Artefacto 2010-06-23 14:46:02

+0

hi Artefacto,done;) – Rolf 2010-06-23 14:55:01

+0

@Rolf Works罰款這裏:http://codepad.viper-7.com/qzOJNL嘗試更新PHP – Artefacto 2010-06-23 15:14:10

回答

0

實際上,__toString只是當變量正在鍵入到字符串,或者該方法本身被稱爲所謂的方法。它表現爲任何其他類的方法,因此,這段代碼必須工作:

class Foo { 
    protected $message = "bar"; 

    public function __toString() 
    { 
     return $this->message; 
    } 
} 

或者你可能在某些__get問題,請張貼它抗衡,我將修改我的答案。

+0

嗨Mikulas,現在發佈:) – Rolf 2010-06-23 14:58:14

0

OK,問題是從別的地方,我有這樣的事情

公共職能setDisplayAll(布爾$值),它似乎是對方法參數此布爾分型觸發一個錯誤(這個類是用於管理錯誤...)

抱歉,感謝您的幫助,並祝您有美好的一天!

相關問題