I am a little confused with extending classes here. In the example below, I am trying to accomplish a way to create a class (a database). Now my database can have tables, I am defining them in the class table below. Finally, I have an event handler (error handler) class. I am trying to let this class determine the $dbhost, $dbuser, and $dbpass, along with the table name.
I want to do this because I want to eventually create another class, lets say 'user', which allows me to access possibly a different database (or even the same database (whatever I define in the class 'user'), and a different tablename.
Now the problem I am having is that I don't understand how to set the values in the grandparent class. Obviously $this->dbhost = "localhost"; is not correct. When calling a method, $this->dbconnect(); from inside the eventHandler class works fine, but accessing the variable does not. Also when I was reading through the php documentation, I noticed something like: parent::dbhost = "localhost". Maybe this is the key, but in this instance it would be a grandparent class that I am trying to affect, not a parent class. I am trying to accomplish this without instantiating the parent classes, maybe thats the problem. If someone could please explain this a little better to me that would be greatly appreciated.
-- here's a sample of what I am TRYING to do. --
class db {
var $link;
var $dbhost;
var $dbuser;
var $dbpass;
function db() {
// constructor method here
}
function dbconnect() {
$this->link = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass);
}
}
class table extends db {
var $tablename;
function table() {
// constructor method here
}
// other methods here
}
class eventhandler extends table {
var $eventdata;
function eventhandler() {
$this->dbhost = 'localhost';
$this->dbuser = 'username';
$this->dbpass = 'password';
$this->dbconnect();
}
}