试图获得非对象的属性

时间:2015-07-29 01:23:15

标签: php mysql

我无法在我的PHP代码中选择数据 这是我的代码

home.php

<div class="row">
  <div class="col-sm-8">
<?php foreach (App::getInstance()->getTable('Article')->last() as $post) :?> 
       <h2> <a href="<?= $post->url; ?>"><?= $post->titre; ?></a></h2>
       <p><em><?= $post->categorie; ?></em></p>
       <p><?= $post->extrait; ?></p>
    <?php endforeach; ?>
  </div>

articleTable.php

<?php
namespace app\Tables;
use core\Tables\Table;

class ArticleTable extends Table{
    public function last(){
        return $this->query('select *from articles');
    }
}

app.php

<?php
use core\config;
use core\Database\mySQLDatabase;

class App{

    private static $_instance;
    private $db_instance;
    public static function getInstance(){
        if(is_null(self::$_instance)){
            self::$_instance = new App();
        }
        return self::$_instance;
        }
    public function getTable($name){
        $class_name = '\\app\\Tables\\' . ucfirst($name) . 'Table';
        return new $class_name($this->getDb());
    }
    public function getDb(){
        $config = Config::getInstance(ROOT . '\config\config.php');
        if(is_null($this->db_instance)){
$this->db_instance = new MySQLDatabase($config->get('db_name'),$config->get('db_user'),$config->get('db_pass'),$config->get('db_host'));
        }
        return $this->db_instance;
    }
}

程序告诉我这个错误:

  

注意:尝试获取非对象的属性   第7行的C:\ wamp \ www \ phpoo(1)\ pages \ articles \ home.php   调用堆栈   TimeMemoryFunctionLocation 10.0006252616 {main}().. \ index.php:0 20.0019307472   要求(&#39; C:\ wamp \ www \ phpoo(1)\ pages \ articles \ home.php&#39;).. \ index.php:23&#34;

有人能告诉我什么是错的吗?

1 个答案:

答案 0 :(得分:0)

$post可能不是那样的对象。尝试将其作为数组索引,例如$post['extrait']