Jump to content

Cannot redeclare class


jeboy

Recommended Posts

Hi! I'm having problem with my web page. Is it a bad practice to declare new object

[ I included a database connection class via include() ] on my index page then Include a function file [ currency.php ] to it that also declare its own object (database connection class)?

 

 

// ---------------------------------

// Database Class [ db.inc.php ]Begin

// ---------------------------------

class db_class{

var $host = '***';

var $user = '***';

var $pass = '***';

var $db = '***';

 

function db_class(){

// Connect to database

$conn = @mysql_connect($this->host,$this->user,$this->pass) or die ('Unable to connect to     

                database');

 

// Select database to use

mysql_select_db($this->db) or die ('Unable to select database');

} // Constructor

 

function query($sql){

$result = mysql_query($sql) or die ("Error in query: $sql " . mysql_error());

return $result;

} // query()

 

} // class db

// ---------------------------------

// Database Class [ db.inc.php ] End

// ---------------------------------

 

 

// ---------------------------------

// Currency Function [ currency.php ] Begin

// ---------------------------------

function set_currency($pref, $amount){

// format dollar currency

if ($pref == 0){

// include db

include('path to --> db.inc.php');

$db = &new db_class;

 

// create query to get forex rate

$sql = 'SELECT rate FROM forex';

$result_rate = $db->query($sql);

$forex = mysql_fetch_object($result_rate);

$rate = $forex->rate;

 

// convert peso to dollar

$dollar = ($amount / $rate);

$dollar_amount = 'USD '.number_format($dollar, 2, '.', ',');

 

// return dollar amount

return $dollar_amount;

 

}elseif ($pref == 1){

// format peso currency

$peso_amount = 'PHP '.number_format($amount, 2, '.', ',');

 

// return peso amount

return $peso_amount;

}

}

// ---------------------------------

// Currency Function [ currency.php ]End

// ---------------------------------

 

 

// ---------------------------------

// Index Page [ index.php ] Begin

// ---------------------------------

include('path --> db.inc.php'); // db_class --database connection class

include('path --> currency.php'); // set_currency() --function

 

// create connection object

$db = &new db_class;

 

// display amount in dollar currency

echo "<font size=2>".set_currency(1, 1000)."</font>";

// ---------------------------------

// Index Page [ index.php ] End

// ---------------------------------

Link to comment
https://forums.phpfreaks.com/topic/50441-cannot-redeclare-class/
Share on other sites

include('path --> db.inc.php'); // db_class --database connection class
include('path --> currency.php'); // set_currency() --function

Its not possible you can include a file or all files in a dir. But you cant include a specificik class. think how would PHP know that this class is in which file ??

you need to use include('db.inc.php'); and include('currency.php');

i dont think that's what he's asking

 

 

as i understand it, you want to know if you should redeclare the class as $db inside the set_currency function or any other, right?

 

 

if your db and currency are always includes to index.php, then no you don't have to redeclare it...

 

 

just put

 

global $db;

 

at the beginning of your set_currency function and reference the class like $db->whatever inside the function, assuming the class has already be instantiated in index

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.