繁体   English   中英

PHP OOP-toString不起作用

[英]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属性似乎也是一个数组,因此您不能简单地返回它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM