Php 如果存在_构造函数,则调用null上的成员函数prepare()
我最近遇到了分页的需要,我将经常使用它,因此我认为类将是一个完美的选择,出于某种原因,当我尝试使用Php 如果存在_构造函数,则调用null上的成员函数prepare(),php,pdo,constructor,Php,Pdo,Constructor,我最近遇到了分页的需要,我将经常使用它,因此我认为类将是一个完美的选择,出于某种原因,当我尝试使用\u构造时,我得到了致命错误: 在null上调用成员函数prepare() 在第31行的“\App\Helpers\PaginationHelper.php”中抛出 第31行是这样的: $sth=$this->db->prepare('SELECT*FROM.$this->table_name.'ORDER BY id LIMIT:page_first_result,:per_page') 在ret
\u构造时,我得到了致命错误:
在null上调用成员函数prepare()
在第31行的“\App\Helpers\PaginationHelper.php”中抛出
第31行是这样的:
$sth=$this->db->prepare('SELECT*FROM.$this->table_name.'ORDER BY id LIMIT:page_first_result,:per_page')代码>
在retrieveData函数中
这是一个很大的混乱,因为我才刚刚开始,但就我的一生而言,我无法找出这个错误,我读过一些其他问题,有相同的错误,但它们都与PDO不连接有关,不像这个问题
\Core\Model只是扩展了一个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;
}
}
我是这样称呼这个班的:
下面是我如何使用函数集_data调用该类:
$pagination = new PaginationHelper();
$pagination->set_data($array_data);
$pagination->table_name('recipe');
$pagination->results_per_page(20);
$pagination->getResults();
当您扩展一个类并在其中包含一个\u构造函数()
函数时,您还应该调用其父类的构造函数:
parent::__construct();
如果跳过这一行,则永远不会调用\Core\Model的构造函数。如果删除构造函数,则会自动调用它。当扩展类并在其中包含\u构造函数()
函数时,还应调用其父类的构造函数:
parent::__construct();
如果跳过这一行,则永远不会调用\Core\Model的构造函数。如果删除构造函数,则会自动调用它。如果定义自己的构造函数,则不会自动调用父级的构造函数
尝试这样做:
public function __construct($sd){
parent::__construct();
$this->set_data = $sd;
}
直接引用文档():
注意:如果子类定义构造函数,则不会隐式调用父构造函数。为了运行父构造函数,需要在子构造函数中调用parent::\uu construct()。如果子类没有定义构造函数,那么它可以像普通类方法一样从父类继承(如果它没有声明为private)
如果定义自己的构造函数,则不会自动调用父级的构造函数
尝试这样做:
public function __construct($sd){
parent::__construct();
$this->set_data = $sd;
}
直接引用文档():
注意:如果子类定义构造函数,则不会隐式调用父构造函数。为了运行父构造函数,需要在子构造函数中调用parent::\uu construct()。如果子类没有定义构造函数,那么它可以像普通类方法一样从父类继承(如果它没有声明为private)
试试这个
public function __construct($sd){
parent::__construct();
$this->set_data = $sd;
}
手动调用父构造函数试试这个
public function __construct($sd){
parent::__construct();
$this->set_data = $sd;
}
手动调用父构造函数您需要正确地启动$this->db
,可能是在构造函数中。目前,它只是一个未初始化(且未声明)的属性\Core\Model从其数据库类中的构造函数中传递$this->db
。错误消息明确表示不是这样。必须显式调用父构造函数才能运行它。您需要正确地启动$this->db
,可能是在构造函数中。目前,它只是一个未初始化(且未声明)的属性\Core\Model从其数据库类中的构造函数中传递$this->db
。错误消息明确表示不是这样。必须显式调用父构造函数才能运行它。