I'm doing a project with PHP OOP. But I'm getting error: Fatal error: Call to a member function prepare() on a non-object in ..../PhotoSection.php on line 12. The below is my source code.
class DatabaseAdapter for connect to database
<?php
class DatabaseAdapter {
protected $_pdo;
function connect() {
if (!$this->_pdo) {
try {
$this->_pdo = new PDO("mysql:host=localhost;dbname=xxx", "xxx", "123456");
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_pdo->exec("SET CHARACTER SET utf8");
$this->_pdo->exec("set character_set_client='utf8'");
$this->_pdo->exec("SET character_set_results='utf8'");
$this->_pdo->exec("SET collation_connection='utf8_general_ci'");
} catch (PDOException $e) {
echo $e->getMessage();
exit;
}
}
}
function disconnect() {
$this->_pdo = null;
}
}
class DatabaseBusiness
require 'DatabaseAdapter.php';
class DatabaseBusiness extends DatabaseAdapter {
protected $_table = "";
protected $_primaryKey = "";
function __construct() {
parent::connect();
}
function __destruct() {
parent::disconnect();
}
}
class PhotoSection
<?php
class PhotoSection extends DatabaseBusiness {
function __construct() {
$this->_table = "photo_section";
$this->_primaryKey = "id";
}
function selectAll() {
try {
$stmt = $this->_pdo->prepare("select * from ?");
$stmt->execute(array($this->_table));
} catch (PDOException $ex) {
echo $ex->getMessage();
exit();
}
return $stmt->fetchAll();
}
}
now I include to template to display data
<?php
include_once 'libraries/DatabaseBusiness.php';
include_once 'libraries/PhotoSection.php';
$photoSection = new PhotoSection();
$photos = $photoSection->selectAll();
?>
I've read the questions duplicate but I have declared and initialized _pdo in DatabaseAdapter and extends it, so I dont know why error. thanks for any helping