2017-09-25 86 views
1

我最近遇到了分页的需求,我将使用它很多,所以我觉得一个类将是一个完美的适合,为某种原因,当我尝试使用__construct我得到的致命错误:调用成员函数prepare()null,如果__construct函数存在

Call to a member function prepare() on null Thrown in '\App\Helpers\PaginationHelper.php' on line 31

第31行是这样的:

$sth = $this->db->prepare('SELECT * FROM '. $this->table_name .' ORDER BY id LIMIT :page_first_result, :per_page');

在retrieveData功能

这是一个很大的混乱,因为我刚刚开始,但对于我的生活我无法弄清楚这个错误,我已经阅读了一些其他问题有相同的错误,但他们都与PDO有关连接,不像这个问题。

\核心\型号只是延伸的PDO连接

class PaginationHelper extends \Core\Model { 

    public $results = []; 
    public $tabs = ''; 
    public $set_data; 
    public $table_name; 
    public $results_per_page; 

    public function __construct($sd){ 
     $this->set_data = $sd; 
    } 

    public function table_name($tn){ 
     $this->table_name = $tn; 
    } 

    public function results_per_page($rpp){ 
     $this->results_per_page = $rpp; 
    } 

    public function retrieveData($page_first_result, $per_page){ 
     $sth = $this->db->prepare('SELECT * FROM '. $this->table_name .' ORDER BY id LIMIT :page_first_result, :per_page'); 
     $sth->bindValue(':page_first_result', $page_first_result, PDO::PARAM_INT); 
     $sth->bindValue(':per_page', $per_page, PDO::PARAM_INT); 
     $sth->execute(); 

     return $sth->fetchAll(PDO::FETCH_ASSOC); 
    } 

    public function getResults(){ 
     $number_of_results = $this->set_data; 

     $this->number_of_pages = ceil(count($number_of_results)/$this->results_per_page); 

     // determine which page number visitor is currently on 
     if (!isset($_GET['pagination'])) { 
      $this->page = 1; 
     } else { 
      $this->page = $_GET['pagination']; 
     } 

     $this_page_first_result = ($this->page - 1) * $this->results_per_page; 

     $fetch = $this->retrieveData($this_page_first_result, $this->results_per_page); 

     foreach($fetch as $data){ 
      $this->results[] = $data; 
     } 

     return $this; 
    } 

    public function loopResults(){ 
     return $this->results; 
    } 

    public function getTabs(){ 

     if($this->page == 1){ 
      $this->back = $this->page; 
     } else { 
      $this->back = $this->page-1; 
     } 

     if($this->page == $this->number_of_pages){ 
      $this->next = $this->page; 
     } else { 
      $this->next = $this->page + 1; 
     } 

     $this->tabs .= ' 
      <div class="pagination"> 
       <a class="pagination__buttons-href" href="?pagination='.$this->back.'"><span class="pagination__buttons flaticon-keyboard-left-arrow-button"></span></a> 
       <span class="pagination__current_page"> '. $this->page .' </span> 
       <a class="pagination__buttons-href" href="?pagination='. $this->next .'"><span class="pagination__buttons flaticon-keyboard-right-arrow-button"></span></a> 
      </div> 
     '; 

     return $this->tabs; 
    } 
} 

现在,这里是一些有趣的事情:如果我删除__construct function,创造把$ set_data性质类似下面的新方法,一切完美。

public function set_data($sd){ 
    $this->set_data = $sd; 
} 

这是我如何调用类:

这是我如何打电话与功能set_data类:

$pagination = new PaginationHelper(); 
$pagination->set_data($array_data); 
$pagination->table_name('recipe'); 
$pagination->results_per_page(20); 
$pagination->getResults(); 
+2

您需要正确启动'$ this-> db',可能是在构造函数中。现在它只是一个未初始化的(并且未声明的)属性 – Calimero

+0

\ Core \ Model将数据库类中的构造函数传递给'$ this-> db'。 – Craig

+2

错误信息明确表示否则。必须明确调用父构造函数才能运行它。 – Calimero

回答

3

如果你定义了自己的构造函数,父类的构造函数将不会自动调用。从文档(http://php.net/manual/en/language.oop5.decon.php

public function __construct($sd){ 
    parent::__construct(); 
    $this->set_data = $sd; 
} 

直接报价:

尝试这样做,而不是

Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private).

3

当你扩展一个类和有__constructor()功能,你应该打电话给它的父母的构造函数:

parent::__construct(); 

如果你跳过第是行,\ Core \ Model的构造函数永远不会被调用。如果您删除构造函数,它会自动调用。

1

试试这个

public function __construct($sd){ 
parent::__construct(); 
$this->set_data = $sd; 

}

手动调用父构造函数

相关问题