Page 1 of 1

How do 'you' set config values?

Posted: Tue Nov 13, 2007 5:41 am
by Kadanis
This is a topic I was thinking about and wondered what the you guys thought. I have used a couple of different ways to list things like DB addresses passwords etc and started thinking which was the better/most common/prefered way to do it.

1.
As a file of variables

Code: Select all

$database_host = 'xyz';

$database_user = 'abc';

...etc...
and then just included and used as required

2.
As a class

Code: Select all

class Config {

   public $database_host = 'xyz';

}
called as Config::database_host;

3.
As a file of constants

Code: Select all

define("HOST", "xyz");

Any other suggestions or comments?

Posted: Tue Nov 13, 2007 6:04 am
by Kieran Huggins
I think we JUST had this topic a week or two ago.

Does anyone remember how to set $_SERVER or $_ENV variables in apache? I could swear I've done it before, but I can't find any code :-(

Posted: Tue Nov 13, 2007 6:10 am
by Kadanis
*blush* my bad for not using the search then :( have been away from the forums etc for a month-ish...

Posted: Tue Nov 13, 2007 6:23 am
by sunilbhatia79
Hi Kadanis,

In your second example I believe that the code should be as follows:

Code: Select all

class Config {

   const HOST = 'xyz';

} 

$host =  Config::HOST;
You cannot use a class level variable using Config::database_host directly without creating an object unless you declare it static. Further, creating constants will help you save variable memory space.

Other methods of configuring your application could be an XML file that would store name/value pairs. But then special care should be taken that your XML file is secure.

Posted: Tue Nov 13, 2007 2:25 pm
by alex.barylski
Kieran Huggins wrote:I think we JUST had this topic a week or two ago.

Does anyone remember how to set $_SERVER or $_ENV variables in apache? I could swear I've done it before, but I can't find any code :-(
http://httpd.apache.org/docs/2.0/mod/mo ... ml#passenv

Posted: Tue Nov 13, 2007 3:49 pm
by pickle
The best place to do it (from what I've found) is in a config file that is included.

Globals cannot be unset, so are available in absolutely any script that includes the global-defining script.
Storing them in public config variables means they'll be outputed if the object is displayed. Make them private & they should be ok.

Posted: Wed Nov 14, 2007 6:20 am
by jmut
pickle wrote:The best place to do it (from what I've found) is in a config file that is included.

Globals cannot be unset, so are available in absolutely any script that includes the global-defining script.
Storing them in public config variables means they'll be outputed if the object is displayed. Make them private & they should be ok.
uh?! I think you are very very mistaken...or maybe I didn't get something :)
1. globals is a baaad thingy...I don't see how cannot be unset, even if so..they can be overwritten - with wrong values
2. public config variables....output if object displayed?! how will private help in this way....

Posted: Wed Nov 14, 2007 11:51 am
by pickle
jmut wrote:
pickle wrote:The best place to do it (from what I've found) is in a config file that is included.

Globals cannot be unset, so are available in absolutely any script that includes the global-defining script.
Storing them in public config variables means they'll be outputed if the object is displayed. Make them private & they should be ok.
uh?! I think you are very very mistaken...or maybe I didn't get something :)
1. globals is a baaad thingy...I don't see how cannot be unset, even if so..they can be overwritten - with wrong values
2. public config variables....output if object displayed?! how will private help in this way....
Whoops

1) I mis-typed here. I meant constants, not globals. Variables you set with define() cannot have their value set & are available anywhere.
2) I assumed that when you declare an object variable private, it's value wouldn't be output when calling print_r() or var_dump(). I was evidently wrong.

Thanks for pointing out the mistakes there.

Nonetheless, my initial point is still valid - the best place to put it is in a config file that is included.

Posted: Wed Nov 14, 2007 12:02 pm
by s.dot
I use the include file as well.

However, if it's something I might wish to change at some point, like an option on the web site, I may do something like..

Code: Select all

define('REGISTRATION_EMAIL_AUTH', true);

Posted: Wed Nov 14, 2007 12:32 pm
by Christopher
I use a Config class and make it available via a Registry. That way the interface is consistent, but the source of the config data can be in any format or multiple formats.