从扩展类调用其对象时 PDO rowCount() 错误

发布于 2024-10-22 02:43:45 字数 2515 浏览 4 评论 0原文

我制作了一个数据库类,它使用 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 中尝试我的网站

http://www.helixagent.com

我似乎是在 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

http://www.helixagent.com

I seem to be ok in Firefox 3.6 but all those other browsers throw me the error I mentioned...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

念三年u 2024-10-29 02:43:45

将您的 row_count-method 更改为:

public function row_count($statement)
{
  self::$PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  try {
    $stmt = self::$PDO->query($statement); 
    $result = $stmt->rowCount();
    return $result;
  } catch (PDOException $e) { echo $e->getMessage(); }

    return false;
}

现在您至少会知道出了什么问题。

Change your row_count-method to this:

public function row_count($statement)
{
  self::$PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  try {
    $stmt = self::$PDO->query($statement); 
    $result = $stmt->rowCount();
    return $result;
  } catch (PDOException $e) { echo $e->getMessage(); }

    return false;
}

Now you'll at least get an idea what is wrong.

霞映澄塘 2024-10-29 02:43:45

您的构造函数应称为 __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.

乙白 2024-10-29 02:43:45

此摘录来自此处(http://php.net/manual/en/language .oop5.basic.php)

$this 和 self 有什么区别?

在类定义中,$this 指的是当前对象,而 self 指的是当前类。

需要使用 self 引用类元素,并使用 $this 引用对象元素。

因此,在函数 row_count(...) 中,您应该使用 $this->PDO->query(...

[编辑]

您可以尝试在调用之前在子类中声明 $this->db = null;你的父类构造。

class MyOtherClassB extends MyClass
{
    public __construct()
    {
        $this->db = null;
        parent::__construct();
    }

This extract is from here (http://php.net/manual/en/language.oop5.basic.php)

What is the difference between $this and self ?

Inside a class definition, $this refers to the current object, while self refers to the current class.

It is necessary to refer to a class element using self, and refer to an object element using $this .

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.

class MyOtherClassB extends MyClass
{
    public __construct()
    {
        $this->db = null;
        parent::__construct();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文