从扩展类调用其对象时 PDO rowCount() 错误
我制作了一个数据库类,它使用 PDO 连接到我的查询,我在主类构造函数中将其作为对象调用。
class MyClass
{
protected $db;
public __constructor()
{
$this->db = new Database();
}
}
class Themes extends MyClass
{
public $avatar;
public $theme_name;
public $theme_by;
public $theme_by_email;
public $theme_by_website;
public $theme_description;
public $theme_thumb;
public $theme_source;
public $theme_css;
public $theme_js;
public $theme_uploaded_on;
public function __construct()
{
parent::__construct();
$this->get_theme();
$this->get_avatar();
}
public function get_theme()
{
$sql = "SELECT *
FROM `user_themes`
WHERE `user_id` = " . $this->session->get('user_id');
if($this->db->row_count($sql))
{
$result = $this->db->fetch_row_assoc($sql);
$this->theme_name = $result['theme_name'];
$this->theme_by = $result['theme_by'];
$this->theme_by_email = $result['theme_by_email'];
$this->theme_by_website = $result['theme_by_website'];
$this->theme_description = $result['theme_description'];
$this->theme_source = $result['theme_source'];
$this->theme_css = $result['theme_css'];
$this->theme_js = $result['theme_js'];
$this->theme_uploaded_on = $result['theme_uploaded_on'];
}else{
die('no results');
}
}
}
我的问题是,如果我包含调用 MyClass 构造函数的扩展类,那么我会收到此错误:
Fatal error: Call to a member function rowCount() on a non-object in db.class.php on line 98
它指向我的 db.class.php 中的这一行
class Database {
private static $PDO;
private static $config;
public function __construct() {
if (!extension_loaded('pdo'))
die('The PDO extension is required.');
self::$config = config_load('database');
self::connect();
}
...
public function row_count($statement)
{
return self::$PDO->query($statement)->rowCount(); //Line 98
}
}
如果我从扩展类中注释掉parent::__construct() 那么我没问题,没有错误。
在 FireFox、Chrom、Opera 和 Safari 中尝试我的网站
我似乎是在 Firefox 3.6 中没问题,但所有其他浏览器都会向我抛出我提到的错误...
I have a database class that I made that uses PDO to connect to my queries, I'm calling it in my main classes constructor as an object.
class MyClass
{
protected $db;
public __constructor()
{
$this->db = new Database();
}
}
class Themes extends MyClass
{
public $avatar;
public $theme_name;
public $theme_by;
public $theme_by_email;
public $theme_by_website;
public $theme_description;
public $theme_thumb;
public $theme_source;
public $theme_css;
public $theme_js;
public $theme_uploaded_on;
public function __construct()
{
parent::__construct();
$this->get_theme();
$this->get_avatar();
}
public function get_theme()
{
$sql = "SELECT *
FROM `user_themes`
WHERE `user_id` = " . $this->session->get('user_id');
if($this->db->row_count($sql))
{
$result = $this->db->fetch_row_assoc($sql);
$this->theme_name = $result['theme_name'];
$this->theme_by = $result['theme_by'];
$this->theme_by_email = $result['theme_by_email'];
$this->theme_by_website = $result['theme_by_website'];
$this->theme_description = $result['theme_description'];
$this->theme_source = $result['theme_source'];
$this->theme_css = $result['theme_css'];
$this->theme_js = $result['theme_js'];
$this->theme_uploaded_on = $result['theme_uploaded_on'];
}else{
die('no results');
}
}
}
My problem is that if I include my extended classes that calls the constructor of MyClass then I get this error:
Fatal error: Call to a member function rowCount() on a non-object in db.class.php on line 98
which points to this line in my db.class.php
class Database {
private static $PDO;
private static $config;
public function __construct() {
if (!extension_loaded('pdo'))
die('The PDO extension is required.');
self::$config = config_load('database');
self::connect();
}
...
public function row_count($statement)
{
return self::$PDO->query($statement)->rowCount(); //Line 98
}
}
If I comment out parent::__construct() from my extended classes then I'm ok and get no errors.
Try my site in FireFox, Chrom, Opera, and Safari
I seem to be ok in Firefox 3.6 but all those other browsers throw me the error I mentioned...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将您的 row_count-method 更改为:
现在您至少会知道出了什么问题。
Change your row_count-method to this:
Now you'll at least get an idea what is wrong.
您的构造函数应称为
__construct()
,而不是__constructor()
。因此,您的代码在调用父构造函数时可能会失败。此外,当 query() 方法失败时,将返回 false,而不是返回可以调用 rowcount() 的对象。您应该添加一个检查来查看查询是否成功。
Your constructor should be called
__construct()
, not__constructor()
. So your code likely fails when calling the parent constructor.Also, when the query()-method fails, false is returned instead of an object on which you can invoke rowcount() on. You should add a check to see if the query is successful.
此摘录来自此处(http://php.net/manual/en/language .oop5.basic.php)
因此,在函数 row_count(...) 中,您应该使用 $this->PDO->query(...
[编辑]
您可以尝试在调用之前在子类中声明 $this->db = null;你的父类构造。
This extract is from here (http://php.net/manual/en/language.oop5.basic.php)
Therefore in your function row_count(...) you should use $this->PDO->query(...
[EDIT]
You could try declare $this->db = null; in your child classes before you call your parent class construct.