[英]PHP oop inheritance of PDO connection to mysql database
I created two classes , USER
and ADMIN
, and admin extends user. 我创建了两个类
USER
和ADMIN
,并且admin扩展了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. 使用
USER
类的对象时,我可以从数据库中获取所需的数据,但是使用ADMIN
对象时,则无法获取任何数据。 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: 起初我没有包括构造函数,因为我读到admin将继承每个非
private
属性,因此这是我的第二次尝试,但是在两种情况下,我都收到此错误:
Call to a member function prepare() on a non-object
any idea what am i missing ? Call to a member function prepare() on a non-object
任何想法我想念什么? thx 谢谢
UPDATE: i have this function for example in ADMIN
class: 更新:我在
ADMIN
类中具有以下功能:
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 这应该可以说明您的问题http://php.net/manual/zh/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();
但是,将其设置为
protected
将允许它从任何扩展USER的类中访问,因此假定您从ADMIN类运行父构造parent::__construct();
如parent::__construct();
ADMIN将可以看到它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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.