views:

69

answers:

3

Hello,

I currently have a list of defined constants and a function that regex'es every pulled MySQL string and looks for things like CLIENT_NAME, LOCAL_API_ADDRESS and auto-changes it.

// several fields
define ('CLIENT_NAME', '...');
define ('LOCAL_API_ADDRESS', '...');
...

The thing is, as my app is getting larger I feel this is pretty inefficient and bound to loose strings (forgetting to regex here or there).

How do you guys cope with this?

+1  A: 

MySQL has had User-defined variables since v3.23.6, but they...

...are connection-specific. That is, a user variable defined by one client cannot be seen or used by other clients. All variables for a given client connection are automatically freed when that client exits.

Effectively, there's no convention in MySQL for storing constants.

You could store the values in a table, but things like MySQL's REGEXP might require dynamic SQL (using MySQL's Prepared Statements) in order to take advantage of the setup.

OMG Ponies
@OMG Ponies, actually I went with Aaron's way (my way) and will keep REGEX'ing things. Just in a more controlled way and keeping a site-wide class for it (to minimize loose ends). But you're response was more on target.
Frankie
+3  A: 

Honestly, unless you are doing this an abnormally large number of times, it's not that bad to do this. Many templating engines uses regex search and replaces. Most of the overhead is in loading the regex engine the first time. Multiple queries aren't that expensive.

You are better off ensuring that your REGEX's are optimized.

Aaron Harun
@Aaron, thank you for your time! Will keep the regex way.
Frankie
+1  A: 

Be careful handling data like this. If you pull data from the database that overrides certain constants, you might end up with security issues !

wimg
@wimg thank you for the input. Actually all constants stored in the database get there only by the site's admin (me). So its really not a security issue. They are done that way so I can share a code-base with several different sites. Nevertheless it's always good to point loose ends.
Frankie