Re: [PHP] Discussion of method -- config files
On Thu, Jun 19, 2008 at 10:04 AM, tedd [EMAIL PROTECTED] wrote: Hi gang: More of a question of method rather than of right or wrong -- of the two methods mentioned here, which way would be better and why? 1. Setting $GLOBALS one time as shown here. At 12:23 AM -0400 6/19/08, Robert Cummings wrote: And the variables are defined in config.php -- config.php -- ?php //Mysql vars $GLOBALS['mysql_host'] = localhost; $GLOBALS['mysql_user'] = [EMAIL PROTECTED]; $GLOBALS['mysql_pass'] = ; ? 2. Or, setting variables as shown below and including them when needed? -- config.php -- ?php //Mysql vars $localhost = 'localhost'; $mysql_user = '[EMAIL PROTECTED]'; $mysql_pass = ''; ? Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php I use a static class for my config. I know using an array would be faster but I need to have consistency between the projects. An example would be: $_cfg = array( 'local.writable.path' = $localPath .'writable/', 'local.template.path' = $localPath .'templates/', 'root' = '/' ); ... $_cfg['fck.editor.url'] = $_cfg['currentHost'].'lib/FCKEditor/'; $_cfg['fck.config'] = $_cfg['currentHost'].'lib/FCKEditor/'; $_cfg['fck.config.js.url'] = $_cfg['currentHost'].'fckconfig.js.php'; blah_Config::setMany($_cfg); Then $root = blah_Config::get('root'); Out of the two methods that you described, I would go with the $GLOBALS. Although I would make sure to put my config into a separate array so that there is less chance of collisions. The reason I would use the globals array is because it is very obvious that you are using a global variable that came from somewhere else. Plus there is a much smaller chance that you will create a $GLOBALS entry for a temp variable that would overwrite your $db_host variable. $GLOBALS['config']['db_host'] = 'localhost'; If it is only you working on your project never using any outside code it really doesn't matter though. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Discussion of method -- config files
Hi gang: I'm not big on globals, but that may be better than including a config file every time a variable is needed. And considering that these variables are not really variables, but more of the static variety, then constants are a consideration. Thanks for all the things to consider. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Discussion of method -- config files
Hi tedd My final solution was. class conf { public static $vals = array(); // config array ... } conf::$vals['mysql_host'] = 'localhost'; conf::$vals['mysql_user'] = [EMAIL PROTECTED]; you can access anywhere of your script and you won't need to pollute $GLOBALS. Regards Sancar On Thursday 19 June 2008 17:04:17 tedd wrote: Hi gang: More of a question of method rather than of right or wrong -- of the two methods mentioned here, which way would be better and why? 1. Setting $GLOBALS one time as shown here. At 12:23 AM -0400 6/19/08, Robert Cummings wrote: And the variables are defined in config.php -- config.php -- ?php //Mysql vars $GLOBALS['mysql_host'] = localhost; $GLOBALS['mysql_user'] = [EMAIL PROTECTED]; $GLOBALS['mysql_pass'] = ; ? 2. Or, setting variables as shown below and including them when needed? -- config.php -- ?php //Mysql vars $localhost = 'localhost'; $mysql_user = '[EMAIL PROTECTED]'; $mysql_pass = ''; ? Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Discussion of method -- config files
On Thu, 2008-06-19 at 10:04 -0400, tedd wrote: Hi gang: More of a question of method rather than of right or wrong -- of the two methods mentioned here, which way would be better and why? 1. Setting $GLOBALS one time as shown here. At 12:23 AM -0400 6/19/08, Robert Cummings wrote: And the variables are defined in config.php -- config.php -- ?php //Mysql vars $GLOBALS['mysql_host'] = localhost; $GLOBALS['mysql_user'] = [EMAIL PROTECTED]; $GLOBALS['mysql_pass'] = ; ? 2. Or, setting variables as shown below and including them when needed? -- config.php -- ?php //Mysql vars $localhost = 'localhost'; $mysql_user = '[EMAIL PROTECTED]'; $mysql_pass = ''; ? These are sort-of the same... until you include the file from within a function and you find that the latter style has not actually populated the global variables, but rather ha populated local variables in the containing function. Having said that though, both are ugly. In practice I do the first but using a second level of nesting. For instance: $GLOBALS['interJinn']['mysql_host'] = localhost; $GLOBALS['interJinn']['mysql_user'] = [EMAIL PROTECTED]; $GLOBALS['interJinn']['mysql_pass'] = ; That way at least the global space is only polluted by a single well defined variable (unlikely someone else is using a variable named interJinn ;) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Discussion of method -- config files
On Thu, 2008-06-19 at 17:31 +0300, Sancar Saran wrote: Hi tedd My final solution was. class conf { public static $vals = array(); // config array ... } conf::$vals['mysql_host'] = 'localhost'; conf::$vals['mysql_user'] = [EMAIL PROTECTED]; you can access anywhere of your script and you won't need to pollute $GLOBALS. Technically, you've polluted global class namespace with a class called conf. I wonder how many others have made a class called conf? Good thing namespaces are coming along eventually :) Admittedly though, the class implementation is more robust since it could pull it's config information from anywhere. However, it's also less efficient. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Discussion of method -- config files
My preference for years has been like so: ?php // inc/config.php # # Handles basic configuration settings. # // DATABASE SETTINGS // The name of the database. $_DB['name'] = some_database; // The user with access to the above database. $_DB['user'] = some_user; // The password for the above user. $_DB['pass'] = p455w0rD; // The hostname/IP of the database server. $_DB['host'] = localhost; // GENERAL SITE SETTINGS // I'd place these in the database, but this is // for example only here. // The title of the website. $_SS['title'] = Welcome to PHP-General!; // MAIL SETTINGS // This can be useful in a configuration // file to email alerts of DB connect failure. // The site administrator's email address. $_MAIL['admin'] = [EMAIL PROTECTED]; ? Then, in a general file, I include all files that will need to be included across all pages like so: ?php // inc/include_files.php # # A generic include file. # // The base directory of the website. $base = dirname(dirname(__FILE__)); // DO NOT USE include_path FOR // THESE, BECAUSE MULTIPLE // FILES MAY HAVE THE SAME NAME! // Basic includes. include_once($base.'/inc/config.php'); include_once($base.'/inc/db.php'); include_once($base.'/inc/sessions.php'); // Function files. include_once($base.'/func/db.php'); ? And without writing out all files, as I'm sure your imagination should suffice, I'll show the database basics. First, in an include file, define the database connections. For now, I'll show only a single-database configuration, but you can take the hint on how multiple databases work with this. ?php // inc/db.php # # Define the database connections for as many databases as we need. # // Establish the connection with the primary database $_DB['conn'] = mysql_connect($_DB['host'],$_DB['user'],$_DB['pass']) or die ('I couldn\'t connect to the database. Know why? Because MySQL is complaining about something. Does this make sense to you...? :BR /'.mysql_error().\n); ? Then, in func/db.php, I might have a function like so: ?php // func/db.php # # Handle multiple connection routines. # // This is what we'll use to process and output (or otherwise handle) errors. function db_error($function) { $err = MySQL error reached when calling function .$function.(). MySQL said:BR /\n; $err .= FONT COLOR=#FF.mysql_error()./FONTBR /\n; $err .= BR /\n; return $err; } // Primary database connection. function site_query($sql) { // Simply return the connection resource ID // Since we're in a function, we don't automatically have access to $_DB. global $_DB; // This globalizes the $_DB array for use in this function. // Select the site database $site_db = mysql_select_db($_DB['name'],$_DB['conn']); $r = mysql_query($sql,$_DB['conn']) or db_error(__FUNCTION__); return $r; } ? Then, since I'm using index.php as my switch page, the beginning would be something like this: ?php // index.php # # Where the magic happens! # // Generic include. include('inc/include_files.php'); // Include the header, which will read $_SS['title'] without a problem. // To be safe, .tpl's are explicitly unreadable by .htaccess config. include('templates/header.tpl'); // Look up some fake info from our fake database. $sql = SELECT DISTINCT something FROM some_table ORDER BY id ASC LIMIT 0,1; $result = site_query($sql); $row = mysql_fetch_assoc($result); echo $row['something'].br /\n; ? Ignore any errors in here. I don't claim this code will run as-is, because I'm programming it into this message, so it should be considered pseudocode only. This is just an example of how I prefer to do things. -- /Daniel P. Brown Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just $59.99/mo. with no contract! Dedicated servers, VPS, and hosting from $2.50/mo. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Discussion of method -- config files
Well, after reaching this $GLOBALS['live']['current']['c']['modules']['traffics']['url'] = 'blah'; I change my mind to use public static variables. using conf::$vars (or someting like that) was more pratic than using $GLOBALS directly. You can set config each of your classes. Regards Sancar -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Discussion of method -- config files
On Thu, 2008-06-19 at 18:37 +0300, Sancar Saran wrote: Well, after reaching this $GLOBALS['live']['current']['c']['modules']['traffics']['url'] = 'blah'; I change my mind to use public static variables. using conf::$vars (or someting like that) was more pratic than using $GLOBALS directly. You can set config each of your classes. So are you saying you instead do: conf::$vars['current']['c']['modules']['traffics']['url'] = 'blah'; Or: conf::$vars_current_c_modules_traffics_url = 'blah'; I fail to see how conf::x overcomes your problem. Also, why would you write out the entire path like that in a configuration? Why not have the entire configuration contained in one expression? $GLOBALS['live'] = array ( 'current' = array ( 'c' = array ( 'modules' = array ( 'traffics' = array ( 'url' = 'blah', ), ), ), ), ); Then adding an additional entry becomes trivial... for instance adding: conf::$vars['current']['c']['modules']['traffics']['pass'] = 'kret'; Is trivial: $GLOBALS['live'] = array ( 'current' = array ( 'c' = array ( 'modules' = array ( 'traffics' = array ( 'url' = 'blah', 'pass' = 'kret', ), ), ), ), ); For instance... here's how I configure database connections: $GLOBALS['interJinn']['databases'] = array ( 'namedDatabase' = array ( 'type' = 'mysql', 'host' = 'interjinn.com', 'user' = 'user', 'password' = 'password', 'db'= 'relevant_database', ), 'otherNamedDatabase' = array ( 'type' = 'mysql', 'host' = 'localhost', 'socket'= '/var/run/mysqld/mysqld.sock', 'user' = 'luser', 'password' = 'notSoSekret', 'db'= 'other_database', ), 'default' = 'namedDatabase', ); Then in my code I can request a connection by name, or let it fall back to the default, or if necessary request a connection with explicit connection details. I never work with connection information beyond the configuration. It's very convenient to be able to alias a database and not worry about the specifics. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Discussion of method -- config files
tedd wrote: Hi gang: More of a question of method rather than of right or wrong -- of the two methods mentioned here, which way would be better and why? Initially I used to access global variables using the global keyword - function foo() { global $bar; } However if the function code is a bit longer, it gets difficult to see if $bar is still actually used, after some revisions. So I switched to using $GLOBALS. Inside $GLOBALS I store variables, arrays and initiated objects (for instance $DB, for database access) - which I call - $GLOBALS[DB]-connect; I do not consider it polluting - it is there to be used. If your code is part of something bigger, then you need a convention, and if you convention supports this method, it's all right. For some reason I do not like the :: operator, it's visually too unstable :) Iv -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Discussion of method -- config files
At 5:31 PM +0300 6/19/08, Sancar Saran wrote: Hi tedd My final solution was. class conf { public static $vals = array(); // config array ... } conf::$vals['mysql_host'] = 'localhost'; conf::$vals['mysql_user'] = [EMAIL PROTECTED]; you can access anywhere of your script and you won't need to pollute $GLOBALS. Regards Sancar Yeah, but now your code is OOP polluted. :-) Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php