Best way for config files (e.g. translations)?

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
Jammerious
Forum Commoner
Posts: 59
Joined: Sat Jun 27, 2009 11:30 am
Location: Slovenia (EU)

Best way for config files (e.g. translations)?

Post by Jammerious »

Hi,
I was thinking which way is the best to store constants inside config files, especially for translations.
Smarty has this done nicely with a Config class:

Code: Select all

# global
siteName = "mod-forge.org"
pageTitle = "mod-forge.org"
 
[Maintainance]
msg = "The site is currently under maintainance, please try later."
 
[Register]
pageTitle = "User registration"
label_username = "Username"
label_password_1 = "Password"
etc.
But how do you read and "register" all these without much struggle in php? If I load the file into an array then there are lots of operations to check, explode, assign...
How about this:

Code: Select all

<?php
// error codes
define('INPUT_ERROR_VALUE_MISSING', 'This field is required');
define('INPUT_ERROR_VALUE_SHORT', 'Value is too short');
define('INPUT_ERROR_VALUE_LONG', 'Value is too long');
define('INPUT_ERROR_VALUE_WRONG', 'This value is invalid');
define('INPUT_ERROR_INVALID_EMAIL','This is not a valid e-mail adress');
define('INPUT_ERROR_INVALID_URL','This is not a valid URL');
define('INPUT_ERROR_CHECKS_FEW','Not enough options checked');
define('INPUT_ERROR_CHECKS_MANY','Too many options checked');
Then for example if I am on register.php I include /languages/en/register.php with the above in it. Is this way better, performance wise? The one thing I can think of being bad here is that I can have similar stuff on two or more different pages, so maybe inside the register.php translation I could refactor any of the global stuff to let's say /languages/en/input.php and have an include inside /languages/en/register.php instead of in the script.
What do you use? How about XML files?
Btw I dropped smarty, won't be having it in my "back-end", that's why I am looking what to do with the configs... :)
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Best way for config files (e.g. translations)?

Post by andyhoneycutt »

Jammerious wrote:But how do you read and "register" all these without much struggle in php? If I load the file into an array then there are lots of operations to check, explode, assign...
You could use the function parse_ini_file to read in the ini settings into an associative array of your choosing.

I dig your sig btw :)

-Andy
User avatar
Jammerious
Forum Commoner
Posts: 59
Joined: Sat Jun 27, 2009 11:30 am
Location: Slovenia (EU)

Re: Best way for config files (e.g. translations)?

Post by Jammerious »

:)

Ini's look good but there are times when you can't use them since you can't pass them as parameter default values, like

Code: Select all

 
function connect($server = DB_SERVER, $username = DB_USER, $password = DB_PASS, $database = DB_DATABASE){
    try{
        $db_link = new PDO('mysql:host='.$server.';dbname='.$database,$username,$password);
    } catch (PDOException $e){
        DEV_MODE ?  print $e->getMessage() : maintainance();
    }
    return $db_link;
}
 
Or I simply don't know how :) (i read them as $cfg = parse_ini_file("configs/general.ini"); then access with $cfg['db_server'])
For languages I think the best way is to have them in a nice XML file, with defined groups, and then I select only those elements that belong to a group, also it would be easier to manipulate them serverside, like correcting stuff and adding new translations...
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Best way for config files (e.g. translations)?

Post by andyhoneycutt »

Ok, i see where you're coming from. Totally correct. I suppose you could come up with a clever routine for loading them in as constants, however.
User avatar
Jammerious
Forum Commoner
Posts: 59
Joined: Sat Jun 27, 2009 11:30 am
Location: Slovenia (EU)

Re: Best way for config files (e.g. translations)?

Post by Jammerious »

Prior to this I tried reading them using eval('define("$key","$value")'); but I read elsewhere eval() is not recommended (was it for being slow?).
Actually, to think of it, there's very little general config you need to store in flatfiles, after you're connected to the DB you can get those configs when needed. I wonder how this impacts speed. DB should be faster, I think.
Heh, I probably worry too much =)
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Best way for config files (e.g. translations)?

Post by andyhoneycutt »

I think file access stuff, in general, is quicker than database access stuff: fewer layers of obfuscation between request and getting to the data storage.

That said, I generally just have a class of application settings defined as constants. I don't like having flat files lying around full of config info, and I see little need to store all that in a database. While it may be a hackish approach, it works well and it's quite fast.

-Andy
User avatar
Jammerious
Forum Commoner
Posts: 59
Joined: Sat Jun 27, 2009 11:30 am
Location: Slovenia (EU)

Re: Best way for config files (e.g. translations)?

Post by Jammerious »

Well, I've been thinking and maybe for the translations I should have them dynamically in the DB, then I can get each language separately into an array, set it to memcached and on DB updates I should refresh the cache.
For other trivial stuff, I'll keep those in a file, like you mentioned.
Thank you :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Best way for config files (e.g. translations)?

Post by Christopher »

Jammerious wrote::)

Ini's look good but there are times when you can't use them since you can't pass them as parameter default values, like

Code: Select all

 
function connect($server = DB_SERVER, $username = DB_USER, $password = DB_PASS, $database = DB_DATABASE){
 
I really think you should find a better way then having a dependency on constants as default parameters like this. I don't like default connection information in DB classes. I think passing the values, or an array of values to the constructor is probably better. Or extending the base DB class if you must have default values. I would prefer a clean dependency on an external Config object interface or external data.
(#10850)
User avatar
Jammerious
Forum Commoner
Posts: 59
Joined: Sat Jun 27, 2009 11:30 am
Location: Slovenia (EU)

Re: Best way for config files (e.g. translations)?

Post by Jammerious »

arborint, these four constants are in the general config, not in a DB class. Although I see your point... I am not sure what exactly are you suggesting :)
Now I made a DB class that extends PDO (didn't feel the need so far), and I make a new instance by a static method, maybe this is better.
Getting a little off topic though :lol:
Post Reply