The Mask
The Mask

Reputation: 17427

PHP OOP - toString not working

I have this class:

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

I'am getting the following error:

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

why not works?

Upvotes: 0

Views: 447

Answers (1)

Phil
Phil

Reputation: 164740

The magic method name should be

public function __toString()

Even then, your $config property appears to be an array so you can't simply return that.

Upvotes: 3

Related Questions