PHP OOP - toString not working


PHP OOP - toString not working

我有这样一个类:

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

为什么不工作?

魔法方法的名字应该是

public function __toString()

即使这样,你的$config属性似乎是一个数组,所以你不能简单地返回它。