I created two classes , USER
and ADMIN
, and admin extends user.
I am able to get which ever data i need from the database when using object of class USER
but i cant get any data when working with the ADMIN
object. the classes are as follows:
class USER
{
private $conn;
public function __construct()
{
$database = new Database();
$db = $database->dbConnection();
$this->conn = $db;
}
public function runQuery($sql)
{
$stmt = $this->conn->prepare($sql);
return $stmt;
}
...some functions to query the DB
}
and
class ADMIN extends USER
{
private $conn;
public function __construct()
{
$database = new Database();
$db = $database->dbConnection();
$this->conn = $db;
}
...some other functions to query the DB
}
at first i didn't include the constructor since i read that admin will inherit every not private
property so this is my second try but in both cases i got this error:
Call to a member function prepare() on a non-object
any idea what am i missing ? thx
UPDATE: i have this function for example in ADMIN
class:
public function getAppeals($user_id){
$stmt = $this->conn->prepare("SELECT * FROM appeals WHERE lecturer_id = :lecturer_id");
$stmt->execute(array(':lecturer_id' => $user_id));
$userRow = $stmt->fetchall(PDO::FETCH_ASSOC);
return $userRow;
}
the row that generates the error is this:
$stmt = $this->conn->prepare("SELECT * FROM appeals WHERE lecturer_id = :lecturer_id");
This should explain your issue http://php.net/manual/en/language.oop5.visibility.php
A private property is only visible in the class in which it was defined. However, making it protected
will allow it to be accessed from any class that extends USER so it will be visible to ADMIN assuming you run the parent constructor from the ADMIN class like this parent::__construct();
If you make these changes it should work
class USER
{
//private $conn;
protected $conn;
public function __construct()
{
$database = new Database();
$db = $database->dbConnection();
$this->conn = $db;
}
public function runQuery($sql)
{
$stmt = $this->conn->prepare($sql);
return $stmt;
}
...some functions to query the DB
}
class ADMIN extends USER
{
public function __construct()
{
parent::__construct();
}
public function getAppeals($user_id){
$stmt = $this->conn->prepare("SELECT * FROM appeals WHERE lecturer_id = :lecturer_id");
$stmt->execute(array(':lecturer_id' => $user_id));
$userRow = $stmt->fetchall(PDO::FETCH_ASSOC);
return $userRow;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.