PDO rowCount () error when calling its object from extended classes

I have a database class that I created that uses PDO to connect to my queries, I call it in my main class 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 call the MyClass constructor, 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 on the parent :: __ construct () from my extended classes, then I am fine and get no errors.

Try my site on FireFox, Chrom, Opera and Safari

http://www.helixagent.com

It seems to me that everything is fine in Firefox 3.6, but all these browsers throw me an error, which I mentioned ...

+3
source share
3 answers

row_count :

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;
}

, , , .

+1

(http://php.net/manual/en/language.oop5.basic.php)

$this self?

$this , self .

, self, , $this.

row_count (...) $this- > PDO- > query (...

[EDIT]

$this- > db = null; , .

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

__construct(), __constructor(). , , .

, query() , false , rowcount(). , .

0

All Articles