2012-03-14 64 views
0

這是我的_toString()函數。我找不到任何錯誤,但是當我嘗試回顯這個類的obj時說:「可捕獲的致命錯誤:類分數的對象無法轉換爲字符串英寸」,我一直在瀏覽器中出現錯誤。_toString()PHP函數

public function _toString() 
{ 
    $str = ""; 
    if(($this->num/$this->den) >= 1) 
    { 
     $tmp = ((int)($this->num/$this->den)); 
     $str .= "{$tmp} ";  
    } 

    if ($this->num%$this->den != 0) 
    { 
     $tmp =$this->num % $this->den; 
     $str .= "{$tmp}/{$this->den}"; 
    } 
    return $str; 
} 

它可能是我的功能有問題,或者是別的嗎?謝謝。

(這是一小部分類我想在一個字符串來表示$ num是分子和$書房是分母)

+0

你想要什麼功能? – summea 2012-03-14 23:07:30

回答

3

嘗試使用extra underscore你的函數定義的前面:

public function __toString() { 
    // ... 
} 
+0

令人驚歎的工作!謝謝! – 2012-03-14 23:20:12

1

嘗試用雙下劃線:__toString()

1

應該有兩個下劃線,使其魔力。 __toString()