2011-07-28 30 views
0

我有這個類:PHP OOP - 的toString不工作

<?php 

class config { 
    var $config=""; 
    public function __construct($d) { 
     switch(strtolower(trim($d))) { 
      case "sql": 
      $this -> config = array(...); 
      break; 
     } 
    } 

    public function toString() { 
      return $this -> config; 
    } 
} 
?> 

$c = new config("sql");// calling the class 
echo $c; //error 

我'得到以下錯誤:

(!) Catchable fatal error: Object of class config could not be converted to string in .. 

爲什麼不工作?

回答

3

的魔術方法的名稱應該是

public function __toString() 

即使是這樣,你$config屬性似乎是一個數組,所以你不能簡單地返回。