Closed Thread Icon

Topic awaiting preservation: constructor refuses to work in php-class Pages that link to <a href="https://ozoneasylum.com/backlink?for=12667" title="Pages that link to Topic awaiting preservation: constructor refuses to work in php-class" rel="nofollow" >Topic awaiting preservation: constructor refuses to work in php-class\

 
Author Thread
DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 03-25-2003 14:43

As the title says:
I've written a class in php to give me a simple db-interface
(yes, yes, I know there are hundreds of them already, but I need to learn as well)
and it works fine with the exception of one thing.

I'd like it to create the initial connection as I instansiate the class.
I've made a constructor that is supposed to call a method/function inside the class to create the connection
but I still need to call that function separately to set the connection within the class.

What have I done wrong?
Here's the code for the class:

code:
////////////////////// Class for DB-Access /////////////////////////

class cDbInterface {
var $connection;
/*config connection to database*/
var $host = 'localhost';
var $user = '';
var $password = '';
var $db = 'filearchive';
//Variables that will hold
var $result;
var $items = array();

//Constructor, sets a valid connection
function cDbInterface(){
$this-> connection = hookUpDb;
}

//Generates the connection with the database and returns a
//valid connection for the one calling.
function hookUpDb(){
/*connect to the database*/
if(mysql_connect($this->host,$this->user,$this->password)){
$this->connection = mysql_connect($this->host,$this->user,$this->password);
mysql_select_db($this->db,$this->connection);
return $this->connection;
}else{
return mysql_error();
}
}
/////////////////////////////////////////////////////
////////////////////// Generic QueryAskers///////////

//Use when no returned result is excepted.
//returns true or mysql_error.
function doQuery($sql){
if(!mysql_query($sql,$this->connection)){
return mysql_error();
}
return true;
}

//Use this when you need to use custom made SQL-queries on the database.
//Returns a 2 dimensional array with the result.
function doQueryResult($sql){
if($sql != ""){
$this->result = mysql_query($sql,$this->connection);
$item = array();
while($item = mysql_fetch_array($this->result)){
$this->items[] = $item;
}
return $this->items;
mysql_free_result($this->result);
}else{
return mysql_error();
}
}

//Closes connection to the database.
function closeCon(){
mysql_close($this->connection);
return true;
}
}
/////////////////////////////////////////////



And I have to use it like this:

code:
$dbCon = new cDbInterface;
$dbCon->hookUpDb(); //<------ this is the line that I shouldn't need to use...
$results = $dbCon->doQueryResult('SELECT * FROM filetypes');
foreach($results as $result){
print("<p> Arkiv id ".$result['archive_id']." tillåter ".$result['filetype']."-filer");
}
$dbCon->closeCon();



Any tips?
/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 03-25-2003 15:47

Ah, with some mail help from InI and some coffee it's solved

The new and working constructor:

code:
function cDbInterface(){       
$this-> connection = $this->hookUpDb();
}



And here we go, it works!
Whoopeee!
Amazing what a good tip and a break with some coffee can do for actually reading the error-messages...
Now it's crystal clear that it was reaching out for a function outside the class when I was using () and it was looking for an internal variable without them... Doh...

Now I'm happy again.... (dances away in the dark halls...)
/Dan


{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 03-25-2003 17:12

php error messages suck tremendoustly!

I have yet to run across an enviroment that can be so misleading with it's error messages.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 03-25-2003 17:41

Just a tip you've got kinda of a circular route there.

you've got hookUpDb returning $this->connection which cDBinterface uses to set $this->connection

You could just have the constructor function call hookupDB (it doesn't need to set anything) or you can just make the hookupDB function your cDBinterface function



.:[ Never resist a perfect moment ]:.

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 03-25-2003 19:31

Thanx bit, I noticed that and I've changed it to:

code:
function cDbInterface(){
$this->hookUpDb();
}



The circular things came from me rewriting three functions that I always use into one class, plus it's the first working class I've written in PHP so I'm bound to have somethings left to streamline.

I'm pleased though, it works and I'm getting the exact same data structure back regardless of the database design.
This has spurred me into becoming a lot more object oriented in my next version of my CMS.
Thanx all!
/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

« BackwardsOnwards »

Show Forum Drop Down Menu