I have a PDO database connection class which I am using throughout my site.
pdoConnect.php
class pdoConnect {
private $host = "localhost";
private $db = "database";
private $userSelect = "user";
private $passSelect = "XXXXX";
protected $selectInstance = true;
protected $options = [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
function dsn() {
return 'mysql:host=' . $this->host . ';dbname=' . $this->db;
}
function connectSelect() {
if ($this->selectInstance === true) {
$dsn = $this->dsn();
$this->selectConnect = new PDO($dsn, $this->userSelect, $this->passSelect,
$this->options);
}
return $this->selectConnect;
}
}
pagination.php
class pagination {
var $php_self;
var $rows_per_page = 10; //Number of records to display per page
var $total_rows = 0; //Total number of rows returned by the query
var $links_per_page = 5; //Number of links to display per page
var $append = ""; //Parameters to append to pagination links
var $sql = "";
var $debug = false;
var $conn = false;
var $page = 1;
var $max_pages = 0;
var $offset = 0;
var $multi = ""; //Use if multiple pagination instances are needed
function pagination($connection, $sql, $rows_per_page = 10,
$links_per_page = 5, $append = "", $multi = "") {
$this->conn = $connection;
$this->sql = $sql;
$this->rows_per_page = (int)$rows_per_page;
if (intval($links_per_page ) > 0) {
$this->links_per_page = (int)$links_per_page;
} else {
$this->links_per_page = 5;
}
$this->append = $append;
$this->multi = $multi;
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
if (isset($_GET['page'] )) {
$this->page = intval($_GET['page'] );
}
}
function paginate(){
if ($this->sql != null) {
$all_rs = $this->conn->query($this->sql); //this is line 76
$this->total_rows = $all_rs->rowCount();
}
....
}
}
It works fine everywhere else, but, if I instantiate it then pass it as an argument to another class the object is not accessible.
require_once '/classes/pdoConnect.php';
$db = new pdoConnect();
$pdo = $db->connectSelect();
require_once '/classes/pagination.php';
$pages = new pagination( $pdo, $sql, $page_amount, 8, null );
Then I get...
Fatal error: Call to a member function query() on a non-object in /classes/pagination.php on line 76
But the PDO connection object exists!
var_dump($pdo);
object(PDO)#1 (0) { }
The only thing I can think of, is that I have extended the pdoConnect class to a different class in order to gain access to the $selectInstance property. So in my IDE the pdoConnect class says is Overridden. Can someone please tell me what's going on here?
pagination()wherequery()is called.function pagination(tofunction __construct(in the pagination class.