akaAlso akaAlso - 6 months ago 26
PHP Question

object of class cannot be converted to string

I know this is an old question but I am not finding answer. Here is my code:

class SqlConnect implements connectionInterface {
//put your code here
private $datasource=null;
private $credentials = null;

public function __construct() {
$netsConn = new configInit(); // Here where the error is happing
$datasource = $this->$netsConn->getDatabase();
$credentials = $this->$netsConn->getBasicCredentials();
public function connect()
$conn = mysqli_connect($datasource['servername'], $credentials['username'], $credentials['password']);
if (!$conn)
die("Connection failed: " . $conn->connect_error);
} else {
return $conn;


I am then calling the class like

class applicationsController implements Controller {

private $conn = null;

public function __construct() {
$conn = new SqlConnect();

However I am getting the following error:

Catchable fatal error: Object of class configInit could not be converted to string

This error happens in SqlConnect. I have looked through about 6 answers on stackoverflow and most all of them were about trying to echo some objects, which is not my case


The reason you get the error is because of the following 2 lines

netsConn = new configInit(); // Here where the error is happing
$datasource = $this->$netsConn->getDatabase();

First line is fine, but on the second line, you are using $netsConn as an attribute name for the SqlConnect class ($this). For this to work, php has to use the string representation of $netsConn by attempting to call it's implementation of the magic method __toString(). Since this probably does not exist in your configInit() class, you end up with that error.