I need a little help here, please. It shows "Array to String conversion" error in my frontpage.php But if I use a print_r($topics) it displays all the results from the database which is my class Topic doing I think my __toString() method is not working. Could you help me to understand please. Thank you. This is my code.
My frontpage file
<?php
foreach($topics as $topic) {
echo $topic->title;
}
My index file
<?php require('core/init.php'); ?>
<?php
//Create Topic Object
$topic = new Topic;
//Get Templates & Assign Vars
$template = new Template('templates/frontpage.php');
//Assgin Vars
$template->topics = $topic->getAllTopics();
//Display template
echo $template;
My init file
require_once('config/config.php'); // it has the database host, username, password
function __autoload($class_name) {
require_once('libraries/'.$class_name.'.php'); //it has my classes: Template, Topic
}
My class Template
class Template {
protected $template;
protected $vars = array();
public function __construct($template) {
$this->template = $template;
}
public function __get($key) {
return $this->vars[$key];
}
public function __set($key, $value) {
$this->vars[$key] = $value;
}
public function __toString() {
extract($this->vars);
chdir(dirname($this->template));
ob_start();
include basename($this->template);
return ob_get_clean();
}
My Class Topic
class Topic{
//Initialize DB Variable
private $db;
//Constructor
public function __construct() {
$this->db = new Database();
}
//Get All Topics
public function getAllTopics() {
$this->db->query("SELECT topics.*, users.username, users.avatar, categories.name FROM topics
INNER JOIN users
ON topics.user_id = users.id
INNER JOIN categories
ON topics.category_id = categories.id
ORDER BY create_date DESC
");
//Assing Results Set
$results = $this->db->resultset();
return $results;
}