簡體   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