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

    If all your brackets are lining up ok then that all looks A-ok to me - your $this->dbpass = something in your inherited class will work properly, as those properties are now part of it.

      Well I thought I had it right, however the problem seems to be that the variables are not being set, because when it gets to the line $this->dbconnect(), I get the error:

      Access denied for user 'ODBC'@'localhost' (using password: NO) in d:\database.class.php

      Now, when I do: print_r($this->dbuser); (from within the dbconnect method in the database class, it is empty, not set to what I set it to in the eventhandler class where I called the dbconnect method from)

        Give that a whirl

        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);
        	echo '<br />Ooooh I am connecting<br />';
        	$this->link = 'brrrt - pretend connection';
        }
        
        function shout()
        {
        	echo '<pre>'; print_r($this); echo '</pre>';
        }
        }
        
        class table extends db 
        {
        	var $tablename;
        
        function table()
        {
        	// constructor method here
        }
        	// other methods here
        function whoami()
        {
        	echo 'I am: ', $this->tablename, '<br />';
        }
        }
        
        class eventhandler extends table 
        {
        	var $eventdata;
        
        function eventhandler($name)
        {
        	$this->tablename = $name;
        	$this->dbhost = 'localhost';
        	$this->dbuser = 'username';
        	$this->dbpass = 'password';
        	$this->dbconnect();
        }
        }
        
        $e = &new eventhandler('fooo');
        
        $e->shout();
        
        $e->whoami();

          Well, I'll be a monkey's uncle. I found my mistake, everything was working fine with one exception (that I did not put in the example, so you couldn't have known), I was instantiating the object database from within my event handler class (before I defined the variables (other testing purposes)), and forgot to remove it, $db = new database(); (blah blah), I'm an idiot, thanks for your help though.

            Write a Reply...