How do 'you' set config values?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

How do 'you' set config values?

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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 :-(
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Post by Kadanis »

*blush* my bad for not using the search then :( have been away from the forums etc for a month-ish...
sunilbhatia79
Forum Newbie
Posts: 24
Joined: Sun Nov 11, 2007 9:37 pm
Location: Mumbai, India

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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....
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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