Page 1 of 1

Outsite Variable inside a Class

Posted: Wed Aug 20, 2008 3:49 pm
by psurrena
I have a config file that contains variables that differ local vs remote, such as db information.

Code: Select all

if (stristr($_SERVER['HTTP_HOST'], 'local')||(substr($_SERVER['HTTP_HOST'],0,7)=='192.168')){
   $db_address="localhost";
   $db_user="root";
   ....
}
 
In a class I have the config file included and have a function in the class to connect to the db:

Code: Select all

public function dbConnect(){
   mysql_connect($this->db_address,$this->db_user,$this->db_pass) or die (mysql_error());
   mysql_select_db($this->db_database) or die (mysql_error());
}
Do access modifiers reset the variables from the config file? How do I get the two to work together without calling the function like so:

Code: Select all

//This is ok
public function dbConnect($db_address,$db_user,$db_pass,$db_database){      
   mysql_connect($db_address,$db_user,$db_pass) or die (mysql_error());
  mysql_select_db($db_database) or die (mysql_error());
} 
//I don't like this:
$category->dbConnect($db_address,$db_user,$db_pass,$db_database);
 
//I do like this:
$category->dbConnect();
 
??????

Re: Outsite Variable inside a Class

Posted: Wed Aug 20, 2008 4:52 pm
by Christopher
Pass the parameters to the constructor instead.

Re: Outsite Variable inside a Class

Posted: Wed Aug 20, 2008 5:07 pm
by psurrena
Isn't that the same thing only differnent?

Code: Select all

$category = new ClassName($db_address,$db_user,$db_pass,$db_database);
At one point or another, will I have to have the variables listed if they are outside the class? Also, what is the proper way to say what I just asked :D

Re: Outsite Variable inside a Class

Posted: Wed Aug 20, 2008 5:14 pm
by Christopher
That is the proper way. You can also pass in a Config object that know what the server is and returns the correct value accordingly. You could also do some sort of DI to automate the initialization.

Re: Outsite Variable inside a Class

Posted: Thu Aug 21, 2008 8:45 am
by psurrena
like so?

Code: Select all

 
public function dbConnect($db_address="",$db_user="",$db_pass="",$db_database=""){
    if (stristr($_SERVER['HTTP_HOST'], 'local')||(substr($_SERVER['HTTP_HOST'],0,7)=='192.168')){
        $db_address="localhost";
        $db_user="root";
        $db_pass="";
        $db_database="database";
    }else{
        $db_address="remote";
        $db_user="root";
        $db_pass="";
        $db_database="database";
    }   
    mysql_connect($db_address,$db_user,$db_pass) or die (mysql_error());
    mysql_select_db($db_database) or die (mysql_error());
}
 

Re: Outsite Variable inside a Class

Posted: Thu Aug 21, 2008 8:50 am
by psurrena
Also, is it best to have the access modifiers of these variables set as private? I would assume so but thought I'd ask.

Re: Outsite Variable inside a Class

Posted: Thu Aug 21, 2008 11:25 am
by Christopher
psurrena wrote:like so?
Which configuration you are using is not really a concern of a DB object. I would rather move that to Config object and have the DB object depend on that interface.