views:

240

answers:

5

I have a database where I keep my configuration parameters, I want to load the configuration parameters into my application variables only once (or upon specific request to reload the parameters), I also want these variables which holds the configuration parameters to be accessible from all php pages/scripts, the idea is to save hits on the database and improve the application response time. What is the 'classic' php solution to this matter?

+1  A: 

Make a page that sets constants (key word 'define') early in your routines. Just include it wherever needed.

Smandoli
You can keep separate versions of this page for production vs. development server. When loading new work, just don't load that page. A big time-saver.
Smandoli
Thanks EvilChookie, well put. I may use try something like this: (A) Constants, incl. server-dependent, come from 'define's. (B) Static variables that go with the session are stored in a table (for example user details based on logon). But I'm thinking they get their own array for actual use. (C) Variables that need to persist between calls only go into an array.
Smandoli
As for the other 4 answers so far, they are intriguing but nothing I would attempt, simply because I don't have too many pages or too many variables (and I'm lazy and preoccupied ... and the site isn't broken at the moment). For (A,B,C) in comment above, I have a total of about a dozen variables. But Oded might be getting what he needs.
Smandoli
+3  A: 

It seems to me that this is essentially the same as any other caching question. The fact that the content to be cached is configuration parameters rather than say the content of Web pages or user profile information is unimportant from a technical perspective.

So what you have to do is come up with some caching solution, whether it's memcached or just writing static files with the data you want to cache.

The trick here is that you're not caching HTML to be presented to the user but rather database query results, so you'll probably want to look at approaches like this one:

http://devzone.zend.com/article/1258

Rafe
+1. APC is the most common caching solution used for configuration variables, because (1) Every request needs them (2) They're usually the same for every request (3) You want access to them as fast as possible. Word on the street is facebook caches their config variables in APC.You may also consider the "hidef" pecl extension: http://pecl.php.net/package/hidef , but it doesn't appear to be terribly actively maintained.
Frank Farmer
A: 

Like Smandoli's answer, I use a single file that has my configuration.

However, my configuration is actually a multi-dimensional array - meaning I have much greater control over my config - I can change it on the fly if I need to, as well as breaking up the varialbes.

$config['error']['nologin'] = "You're not logged in";
$config['db']['host'] = "localhost";
$config['something']['else'] = "hello world";

Edit: I use a file for values that do not change too much. I do use variables from a database occasionally, but not too often.

My rule of thumb is "If the user doesn't need to change it, load from the file; if they need to change it, then it comes from a database".

EvilChookie
+2  A: 

I like using the Zend_Config_Ini class. Creating separate sections that can extend others is easy, and with Zend_Cache with Zend_Cache_Frontend_File (to check for updates to the .ini file) and a backend (I use APC) that is particularly fast to access to avoid any overhead of re-parsing.

; Production site configuration data
[production]
webhost                  = www.example.com
database.adapter         = pdo_mysql
database.params.host     = db.example.com
database.params.username = dbuser
database.params.password = secret
database.params.dbname   = dbname

; Staging site configuration data inherits from production and
; overrides values as necessary
[staging : production]
;  'database.adapter' is inherited
;  others are overridden
database.params.host     = dev.example.com
database.params.username = devuser
database.params.password = devsecret
Alister Bulman
A: 

I came from the world of C++ the paradigm there was to use a singleton which load the parameters on first (and only) instantiation and export an interface with relevant get'ters (like 'int GetVal(char* key,int &val)' ) the singletone was accessible from all parts of the application, is there anything like that in PHP?

Oded
perhaps! http://google.com/search?q=php+singleton
Stephanie Luther
You have to keep in mind that each request in PHP is basically like firing up a completely new instance of an executable, getting the output, and then letting the executable terminate. A singleton that you set up in one request will not be available to a second request, unless you put it in some sort of cache. A default, vanilla PHP installation does not include any such caching mechanism (short of flat files). You must install one, such as memcached or APC.
Frank Farmer
I'm not up to researching singletons today, but I will add that my VBA background certainly required me to make a huge adjustment (as described by Frank, +1) to the single-state realities of HTTP.
Smandoli