Outsite Variable inside a Class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Outsite Variable inside a Class

Post 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();
 
??????
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Outsite Variable inside a Class

Post by Christopher »

Pass the parameters to the constructor instead.
(#10850)
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: Outsite Variable inside a Class

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Outsite Variable inside a Class

Post 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.
(#10850)
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: Outsite Variable inside a Class

Post 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());
}
 
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: Outsite Variable inside a Class

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Outsite Variable inside a Class

Post 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.
(#10850)
Post Reply