简体   繁体   English

错误-尝试获取非对象的属性

[英]Error - Trying to get property of non-object

I have my Database Class setup 我有数据库类设置

<?php 
class Database
{
   private $host = DB_HOST;
   private $user = DB_USER;
   private $pass = DB_PASS;
   private $dbname = DB_NAME;

   private $dbh;
   private $error;
   private $stmt;

   public function __construct() 
   {
      // Set DSN
      $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
      // Set options
      $options = array(
         PDO::ATTR_PERSISTENT => true, 
         PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
      );
      // Create a new PDO Instance
      try {
         $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
      }
      // Catch any errors
      catch (PDOException $e) {
         $this->error = $e->getMessage();
      }
   }

   public function query($query) {
      $this->stmt = $this->dbh->prepare($query);
   }

   public function bind($param, $value, $type = null)
   {
      if (is_null($type)) {
        switch (true) {
         case is_int($value):
            $type = PDO::PARAM_INT;
            break;
         case is_bool($value):
            $type = PDO::PARAM_BOOL;
            break;
         case is_null($value):
            $type = PDO::PARAM_NULL;
            break;
         default:
            $type = PDO::PARAM_STR;
        }
      }
      $this->stmt->bindValue($param, $value, $type);
   }

   public function execute(){
       return $this->stmt->execute();
   }

   public function resultset(){
      $this->execute();
      return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
   }

   public function single(){
      $this->execute();
      return $this->stmt->fetch(PDO::FETCH_ASSOC);
   }

   public function rowCount(){
      return $this->stmt->rowCount();
   }

   public function lastInsertId(){
      return $this->dbh->lastInsertId();
   }

   public function beginTransaction(){
      return $this->dbh->beginTransaction();
   }

   public function endTransaction(){
      return $this->dbh->commit();
   }

   public function cancelTransaction(){
      return $this->dbh->rollBack();
   }

   public function debugDumpParams(){
      return $this->stmt->debugDumpParams();
   }
}
?>

My Template Class 我的模板类

<?php 
/*
* Template Class
* Creates a template/view object
*/
class Template 
{
   // Path to template
   protected $template;
   // Variables passed in
   protected $vars = array();

   /*
   * Class Constructor
   */
   public function __construct($template) 
   {
      $this->template = $template;
   }

   /*
   * Get Template Variables
   */
   public function __get($key) 
   {
      return $this->vars[$key];
   }

   /*
   * Set Template Variables
   */
   public function __set($key, $value) 
   {
      $this->vars[$key] = $value;
   }

   /*
   * Convert Object To String
   */
   public function __toString() 
   {
      extract($this->vars);
      chdir(dirname($this->template));
      ob_start();

      include basename($this->template);

      return ob_get_clean();
   }   
}
?>

A Topic class 主题课程

<?php 
class Topic
{
   // Init 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");

      // Assign Result Set
      $results = $this->db->resultset();

      return $results;
   }
}

And then my index controller NB: I linked Database to (init.php), as well as autoload all Class with function __autoload($className){ require_once('libraries/'. $className .'.php');} 然后我的索引控制器NB:我将数据库链接到(init.php),并使用__autoload($ className){require_once('libraries /'。$ className。'。php');}自动加载所有Class

<?php require('core/init.php'); ?>
<?php 
// Create the Topic Object
$topic = new Topic;

// Get Template & Assign Variables
$template = new Template('templates/frontpage.php');

// Assign Variables
$template->topics = $topic->getAllTopics();

// Display Template
echo $template;

On the frontpage.php, i try to access a property of the $topics class, it returns the ERROR - Notice: Trying to get property of non-object in C:\\wamp\\www\\talkingspace\\templates\\frontpage.php 在frontpage.php上,我尝试访问$ topics类的属性,它返回错误-注意:尝试获取C:\\ wamp \\ www \\ talkingspace \\ templates \\ frontpage.php中非对象的属性

Below is my sample frontpage 以下是我的示例首页

<?php foreach ($topics as $topic) { ?>
<div class="topic-content">
<h3><a href="topic.php"><?php echo $topic->title; ?></a></h3>
<div class="topic-info">
<a href="topics.php?category=<?php echo $topic->category_id; ?>"><?php echo $topic->name; ?></a> >> 
<a href="topics.php?user=<?php echo $topic->user_id; ?>"><?php echo $topic->username; ?></a> >> Posted on <?php echo $topic->create_date; ?>
<span class="badge pull-right"><?php echo $topic->id; ?></span</div></div>
<?php } ?>

Please can anybody show me what is wrong? 请有人能告诉我什么地方错了吗? I would appreciate any assistance, thanks! 我将不胜感激,谢谢!

You get the results from this line: 您可以从以下行得到结果:

$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);

This returns an array. 这将返回一个数组。

In the template you are accessing them like object with $topic->someField 在模板中,您可以使用$topic->someField像对象一样访问它们

If you want that to work, then you have 2 options: 如果您希望它能正常工作,那么您有两种选择:

  1. Use PDO::FETCH_OBJ 使用PDO::FETCH_OBJ
  2. Use array syntax in the template like $topic['somefield'] 在模板中使用数组语法,例如$topic['somefield']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM