Page 1 of 1

Class clarifications (long one)

Posted: Fri Mar 24, 2006 6:40 am
by rubberjohn
I dont have a problem as such - anymore, I'm starting to get to grips with classes but there are a few questions I hope someone can answer for me that I havn't been able to figure out myself.

I've been using the example viewtopic.php?t=8873.

I have a class file

Code: Select all

class DB_Class {
 
    var $db;

    function DB_Class($hostname ,$dbname, $username, $password) {

	
	echo "hostname " . $hostname . "<BR>";
	echo "dbname " . $dbname . "<BR>";
	echo "username " . $username . "<BR>";
	echo "password " . $password . "<BR>";
	
        $this->db = mysql_connect ($hostname, $username, $password)
            or die ("Unable to connect to Database Server");

        mysql_select_db ($dbname, $this->db)
            or die ("Could not select database");
			
    }

 function fetch($query) {
.....
}


}
and a file of functions

Code: Select all

function getuser(){

require "../../../conn.php";
require_once "./db.class.php";
$dbs = new DB_Class($hostname_myconnection, $database_myconnection, $username_myconnection, $password_myconnection);


		$my_tags = $dbs->fetch("SELECT `*` from users WHERE user_id=2");
	
}
conn.php contains the db connection variables.

I've simplified this but you get the idea. My questions are:

1)Why do the echoed variables in the class file echo twice?

2)Why, if I change

Code: Select all

require "../../../conn.php";
to

Code: Select all

require_once "../../../conn.php";
do they still print out twice but the second set have no variables assigned and throw an undeclared variable warning?

3)Is it not possible to place all of the require files and class instantiating code at the beginning of the file, meaning all of the functions can use them?

Or does each function need to instantiate the class with a unique variable?

As when I tried to do this I got the 'undefined function and call to a member function on a non_object ' (for the fetch function), the 'Cannot redeclare class' error, or a database connection error(which only happened after the sql was put into a function).

I have been reading up on all of this, from this forum too, but i still cant work it out so if anyone can enlighten me that would be great.

rj

Re: Class clarifications (long one)

Posted: Fri Mar 24, 2006 10:02 am
by feyd
rubberjohn wrote:1)Why do the echoed variables in the class file echo twice?
Are you calling get_user() twice?
rubberjohn wrote:2)Why, if I change

Code: Select all

require "../../../conn.php";
to

Code: Select all

require_once "../../../conn.php";
do they still print out twice but the second set have no variables assigned and throw an undeclared variable warning?
Like the code would suggest, the file is only included once. The variables set in the file are either not globaled or the way you are instantiating DB_Class isn't using the globaled variables.
rubberjohn wrote:3)Is it not possible to place all of the require files and class instantiating code at the beginning of the file, meaning all of the functions can use them?
It is possible. You appear to have the files included in get_user() however.
rubberjohn wrote:Or does each function need to instantiate the class with a unique variable?
That is not needed. What you could do instead of using a globaled variable (not so flexible) is pass the instance into the function. Remember to pass by reference.
rubberjohn wrote:As when I tried to do this I got the 'undefined function and call to a member function on a non_object ' (for the fetch function),
For "undefined function," if you tried to call the function from inside the class without the $this-> prefix, yes, that would likely happen. On "call to member function of non-object," typically the object you think is there isn't because it may have been overwritten (in the variable,) not created for some reason, or some other such thing. The reasons vary (simply due to complexity of events that can lead to it,) but it means exactly as it says: the variable you're trying to use as an object isn't an object. What I do when I get this sort of thing is var_dump() or var_export() the variable. I then trace back toward where the variable is created. Often I mispelled something and the error controls caught it and didn't report much of anything, or I forgot to pull the variable in from another class or something.
rubberjohn wrote:the 'Cannot redeclare class' error,
This comes from including the class definition two or more times. It's similar to doing the following. With two declarations, there's no way the engine would know which to use.

Code: Select all

class foo
{
  var $g = '1234';
}
//...
class foo
{
  var $f = 'asdf';
}
rubberjohn wrote:a database connection error(which only happened after the sql was put into a function).
That seems odd unless the error got suppressed in some fashion or the connection got severed after the original connection call. :?