views:

58

answers:

3

On my site I have a config file which has many settings that need to be accessed in many pages, including inside many class files. The config file is included into the header of every page build. Since I will be using a registry method to store some objects in, I am wondering if it would be better for me to store some setting in this class ass well since it will be available to all my other objects? From my experience over the last couple days, it seems accessing CONSTANTS inside of my class files is difficult sometimes. I am just not sure if it would be a good idea as it would need to call several methods to set and get the settings. Is it common practice to store settings in a registry type object? I have seen on some large projects that they have the methods in place to set/get settings but I have never really seen it in action.

+1  A: 

I like to keep things like this in a registry of some sort. Although for me this is because

  1. the majority of my configuration values are actually multiple parts (ie arrays) so to map much of this out using constants would get pretty long winded. Additionally i dont use php for my config files i always use XML or YAML which is then parsed any way so it jsut makes more sense to go ahead and stick them in a registry as opposed to using constant or globals.
  2. It allows for a single api to get these type of values whther its a db connection object or the path to the webroot on the filesystem

With that said i think it really depends on what the values are and how you intend to use them and if they are structure or essentially flat.

prodigitalson
Are you really parsing xml config files for every request?
Kimble
No, normally i cache the results so they only get parsed when expired or the cache is cleared.
prodigitalson
+1  A: 

I use a combination of two approaches:

1: In every class/controller, I always start with a require_once('app_config.php') in which I have code like:

define('APP_SMTP_SERVER', 'mail.company.com');

that I can then reference as a constant.

2: I have a singleton "Registry" class where I can store keys and values. It has two exposed methods, getAttribute($key) and setAttribute($key, $value). When I save it to the database, I serialize the value, so it can store any data type you throw at it (single values or arrays, etc.).

ChrisW
I have been considering #2 as I am working on using a registry class which I will pass into my other classes so I was thinking since it will be visible everywhere it would be a good place to store some settings in. I just wasn't sure about the performance because instead of a variable or a constant 20 times for 20 settings, it would be like 20 method calls to set the settings in my registry and then 20 more method calls just to get them or something like that.
jasondavis
+1  A: 
just somebody
This looks like something similar that I want to do! Not sure about the ini file though, doesn't that reqire more resources since it is hitting the file system on every page build?
jasondavis
File (meta)data is cached by the kernel. Profile, don't speculate.
just somebody