Thursday, May 5, 2016

CodeIgniter: (4096) Object of class CI_Output could not be converted to string

Devs,

Earlier this morning, I ran into this CI Error which puzzled me for a split second.


I did my quick online search and didn't really find any answer on StackOverflow. I read a quick response on the CodeIgniter forum and then figured out my root cause.

Problem:  I was echo'ing the set_output($arg) method.

echo($orgs ? $orgs : $this->output->set_output("Status: FAIL"));

Attempt: The set_output($arg) method already echo's the input arguments to the view. So remove the echo'ing on the method.

$orgs ? echo $orgs : $this->output->set_output("Status: FAIL");

Solution: Since the ternary operator is not identical to an if-then execution, remove the set_output($arg) method. Parenthesis added for easier readability, but unnecessary.

echo ($orgs ? $orgs : "Status: FAIL");

A nice, quick fix and my expected results worked perfectly!

Good day,
G2