I am receiving the following error below on the object’s function in Smarty and I am unaware of how to resolve the issue.
Error:
Catchable fatal error: Object of class users_class could not be converted to string
This is the following object and function of the object I am using.
class users_class
{
public function fetchUser(array $conditions)
{
$db = Core::getInstance();
$sql = "SELECT * FROM ".USERS." WHERE ";
$i=0;
$params = array();
//$where = array();
foreach ($conditions as $column => $value)
{
if (preg_match('/^[a-z-.-_]+$/', $column)) {
if ($i!=0) {
$sql .= " AND ";
}
$sql .= "$column = ?";
$params[] = $value;
$i++;
}
}
//$sql .= implode(' AND ', $where);
//$sql .= " order by title asc";
$res = $db->dbh->prepare($sql);
$res->execute(array_values($params));
return $res->fetch(PDO::FETCH_ASSOC);
}
}
This is the call in Smarty:
{section name=ststval loop=$ststres}
{if $ststres[ststval].type == 2}
{assign var='udatas' value="$userObj->fetchUser(array('id'=>$ststres[ststval].to_id));"}
I solved this by adding the __toString() method to the object. Apparently, the object just has to return a string. This was confusing to me because I didn’t know what it should return and directions were not clear on php.net. For a person learning PHP things need to be explained when asked for help not pointed to the same articles everyone reads when learning PHP. I believe we come asking for help on websites like these because we require explanations from people with higher knowledge. I call it lazy and unhelpful!