M_PI is pi
Constants are good but readonly variables are better. Not that readonly variables exist, I'm talking encapulation and data hiding here.
I used to use a lot of constants for project management:
Someting like this:
Code: Select all
if(isLocal) {
define('databaseName', 'xxx');
define('relativePath', 'xxx/xxx/xxx/');
define('snapshotPath', 'c:/xxx/');
} else {
define('databaseUser', 'xxx');
define('databasePass', 'xxx');
define('databaseName', 'xxx');
define('relativePath', '/');
define('snapshotPath', '/xxx/xxx/xxx/xxx');
}
define('absPath', 'http://'.$_SERVER['HTTP_HOST'].'/'.relativePath);
define('siteName', 'Some project');
define('contactAddress','xxx');
define('version', '0.7');
//define('iconPath', 'placeholders/');
//define('iconPath', 'rva/');
define('iconPath', 'pw/');
define('contactAdminMsg', '<br />Please contact the administrator '.administratorEmail);
define('stndQueryError', 'Error with query: '); // put mysql_error stuff afterwards
define('isLocal', true);
define('stndDoctype', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
define('stndHTMLTag', '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">');
define('ADMIN_PASS','xxxxxx');
define('USER_ADMIN',1);
define('USER_GUEST',2);
// various truncations
define('RECENT_LIMIT',5);
define('RECENT_TRUNC_LEN',43);
define('LST_V_SIZE',60);
define('LST_R_SIZE',60);
define('LST_Q_SIZE',102);
define('LST_Q_STYLESHEET_SIZE',22);
define('ROWS_PER_PAGE',8);
define('MAX_ENTRIES',500);
define('MAX_REPORTED_QUESTIONNAIRES',8);
define('MYSQL_TEXT_SIZE',65500);
define('MYSQL_VARCHAR_SIZE',255);
define('MAX_URL_SIZE',4096);
define('PRESET_NAME_LENGTH',200);
define('ANSWER_LENGTH',MYSQL_VARCHAR_SIZE);
define('TEXTAREA_WIDTH',45);
define('QTID_FREE_TEXT',1);
define('QTID_SMALL_CHOICE',2);
define('QTID_LARGE_CHOICE',3);
define('HEADER_HEADER',1);
define('HEADER_NAV',2);
define('HEADER_BODY',4);
define('HEADER_ALL',7);
// this will be used a couple of times as user-friendly message when null values are encountered
define('UNSPECIFIED_TEXT','<span class="strike">(unspecified)</span>');
*Shudder* those days are behind me now.
Actually a lot of constants like these I've replaced with the database. I store things like max length truncations and limiters and other values in a table so that should there be the need to make them modifible that won't really upset things. I use an object interface to that table to:
- Prevent me from setting them without it being quite deliberate
- Cache them throughout the application to minimize queries
- Only query the ones I need
I'm not completely constant bashing here. A constant might not be as flexible or as clever as my database solution but is one hell of a lot better than a just using literals i.e. no constants or control system at all.