Reputation: 4359
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...
Upvotes: 3
Views: 3845
Reputation: 4044
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.
Upvotes: 1
Reputation: 10563
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();
}
Upvotes: 0
Reputation: 4044
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.
Upvotes: 0