jeboy Posted May 8, 2007 Share Posted May 8, 2007 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 // --------------------------------- Quote Link to comment https://forums.phpfreaks.com/topic/50441-cannot-redeclare-class/ Share on other sites More sharing options...
neel_basu Posted May 8, 2007 Share Posted May 8, 2007 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'); Quote Link to comment https://forums.phpfreaks.com/topic/50441-cannot-redeclare-class/#findComment-247814 Share on other sites More sharing options...
benjaminbeazy Posted May 8, 2007 Share Posted May 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/50441-cannot-redeclare-class/#findComment-247820 Share on other sites More sharing options...
jeboy Posted May 8, 2007 Author Share Posted May 8, 2007 Benjamin, Ok I understand now, I should use global for $db so theres no need to redeclare the connection object in set_currency() function that is previously instantiated from index.php Thank you, its a great help! Quote Link to comment https://forums.phpfreaks.com/topic/50441-cannot-redeclare-class/#findComment-247861 Share on other sites More sharing options...
benjaminbeazy Posted May 8, 2007 Share Posted May 8, 2007 yep, Quote Link to comment https://forums.phpfreaks.com/topic/50441-cannot-redeclare-class/#findComment-247863 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.