Re: [PHP] Database includes
Larry Garfield wrote: On Sunday 26 August 2007, Bruce Cowin wrote: I'm curious as to how everyone organises and includes their classes in PHP5. Then have a config file of some sort in which you specify your DB credentials. There's a variety of ways to do that (ini file, a PHP file with a database url, a PHP file that just has a couple of variables in it, or constants instead, etc.). Pick one you like. Then have your DB connection class read that file's data one way or another and connect as appropriate. If you have to modify anything other than a single config file in order to move your site/app from one server to another, then you have a design flaw. (I'd say that applies for moving the site to a subdirectory on a server too, but that takes a bit more effort.) I'm with Larry on this. Include a constants file at the top of your scripts. In that file you can place a switch block that tests for the $_SERVER['HTTP_HOST']. For each case, place something like: define('DB_NAME', 'my_dev_db'); Other things this is useful for are setting a boolean constant that a class might test in, eg. a toString method or error handling, and email addresses for scripts that phone the client. If you have a bunch of different ones it might be easiest to define dev test email addresses first and, in the switch block, set the others accordingly. So, say you have several email addresses for various things being sent to client employees (sales@, admin@, jobs@, etc.). You can do something like: define('EMAIL_DEV', '[EMAIL PROTECTED]'); ... switch($_SERVER['HTTP_HOST']) { ... case 'dev.myclient.mycompany.com': define('MY_APP_DEBUG', TRUE); define('DB_NAME', 'my_dev_db'); define('EMAIL_SALES', EMAIL_DEV); define('EMAIL_ADMIN', EMAIL_DEV); define('EMAIL_JOBS', EMAIL_DEV); ... break; No muss. No fuss. b -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Database includes
On Tuesday 28 August 2007, brian wrote: If you have to modify anything other than a single config file in order to move your site/app from one server to another, then you have a design flaw. (I'd say that applies for moving the site to a subdirectory on a server too, but that takes a bit more effort.) I'm with Larry on this. Include a constants file at the top of your scripts. In that file you can place a switch block that tests for the $_SERVER['HTTP_HOST']. For each case, place something like: That's bad, too, if you have multiple developers on the project. Just have a separate config file that contains *nothing* but the installation config, and every install has its own copy. -- Larry Garfield AIM: LOLG42 [EMAIL PROTECTED] ICQ: 6817012 If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it. -- Thomas Jefferson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Database includes
Larry Garfield wrote: On Tuesday 28 August 2007, brian wrote: If you have to modify anything other than a single config file in order to move your site/app from one server to another, then you have a design flaw. (I'd say that applies for moving the site to a subdirectory on a server too, but that takes a bit more effort.) I'm with Larry on this. Include a constants file at the top of your scripts. In that file you can place a switch block that tests for the $_SERVER['HTTP_HOST']. For each case, place something like: That's bad, too, if you have multiple developers on the project. Just have a separate config file that contains *nothing* but the installation config, and every install has its own copy. How so? If, by multiple developers, you mean multiple *development domains*, then yes, that'd need tweaking. Otherwise, i fail to see the badness. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Database includes
brian wrote: Larry Garfield wrote: On Tuesday 28 August 2007, brian wrote: If you have to modify anything other than a single config file in order to move your site/app from one server to another, then you have a design flaw. (I'd say that applies for moving the site to a subdirectory on a server too, but that takes a bit more effort.) I'm with Larry on this. Include a constants file at the top of your scripts. In that file you can place a switch block that tests for the $_SERVER['HTTP_HOST']. For each case, place something like: That's bad, too, if you have multiple developers on the project. Just have a separate config file that contains *nothing* but the installation config, and every install has its own copy. How so? If, by multiple developers, you mean multiple *development domains*, then yes, that'd need tweaking. Otherwise, i fail to see the badness. Multiple developers as in more than one person coding the application. You'd be sharing the development environment (database, files etc) - not what you want. Think if your app was in cvs/subversion and you had someone working on the same stuff at the desk next to you. -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Database includes
Chris wrote: Bruce Cowin wrote: I'm curious as to how everyone organises and includes their classes in PHP5. Let's take a simple example that has 3 classes: customer, order, and database. The database class has a base sql db class (I know there is PDO and other things but this class is already written and working) and classes that inherit from the base class for dev, test, and prod, passing the associated logins. The customer and order will both use the appropriate database class depending on which environment its in (e.g., SalesDevDB, SalesTestDB, SalesProdDB). I don't want to have to go into the customer and order class code and change which db class it uses when I move it from dev to test and from test to prod. What's the proper way to handle this? Or am I way off base? Could you just have 3 config files and use the appropriate one in each environment? The settings are in 'config.php'. When it's in dev: cp dev.php config.php when it's in test: cp test.php config.php I use a slightly different approach to prevent the need to mess about with files when moving to production. At the end on config.php I have this... if (file_exists('config_dev.php')) require 'config_dev.php'; In config_dev.php I override anything defined in config.php that needs to be different on the current server. The config_dev.php file is *not* in source control so it doesn't exist in production but each development and test environment has a version that modifies the production environment. There is a hit involved in the call to file_exists, but PHP caches it so the effect is minimal. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Database includes
On 27/08/07, Stut [EMAIL PROTECTED] wrote: I use a slightly different approach to prevent the need to mess about with files when moving to production. At the end on config.php I have this... if (file_exists('config_dev.php')) require 'config_dev.php'; I've got my own variation on this, came to use it to prevent overwriting config files for stage/dev/live or other versions of a site. I've got a general purpose constants file, which defines a lot of constants which are generally used a lot in my applications (file rootdir, DAY, HOUR, database passwords, some regexes, these things). Instead of just defining them there, I define them using a simple conditional define function (if (!defined($constantName) define($constantName, $value)). On top of my constants.inc.php (as I chose to call it) I use the following: if (isset($_SERVER['SERVER_NAME'])) { $_config_file = dirname(__FILE__).'/../eg/'.$_SERVER['SERVER_NAME'].'.inc.php'; @include($_config_file); } The included hostname specific config file would then define some constants, and because they are already defined the default values from constants.inc.php don't get set anymore. Hope this is of help to anybody, it has certainly relaxed my deployment process.. Wouter In config_dev.php I override anything defined in config.php that needs to be different on the current server. The config_dev.php file is *not* in source control so it doesn't exist in production but each development and test environment has a version that modifies the production environment. There is a hit involved in the call to file_exists, but PHP caches it so the effect is minimal. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Interpotential.com Phone: +31615397471
Re: [PHP] Database includes
Thanks to everyone who responded. Some really interesting ideas. I'll try them out. Regards, Bruce Wouter van Vliet / Interpotential [EMAIL PROTECTED] 28/08/2007 8:34 a.m. On 27/08/07, Stut [EMAIL PROTECTED] wrote: I use a slightly different approach to prevent the need to mess about with files when moving to production. At the end on config.php I have this... if (file_exists('config_dev.php')) require 'config_dev.php'; I've got my own variation on this, came to use it to prevent overwriting config files for stage/dev/live or other versions of a site. I've got a general purpose constants file, which defines a lot of constants which are generally used a lot in my applications (file rootdir, DAY, HOUR, database passwords, some regexes, these things). Instead of just defining them there, I define them using a simple conditional define function (if (!defined($constantName) define($constantName, $value)). On top of my constants.inc.php (as I chose to call it) I use the following: if (isset($_SERVER['SERVER_NAME'])) { $_config_file = dirname(__FILE__).'/../eg/'.$_SERVER['SERVER_NAME'].'.inc.php'; @include($_config_file); } The included hostname specific config file would then define some constants, and because they are already defined the default values from constants.inc.php don't get set anymore. Hope this is of help to anybody, it has certainly relaxed my deployment process.. Wouter In config_dev.php I override anything defined in config.php that needs to be different on the current server. The config_dev.php file is *not* in source control so it doesn't exist in production but each development and test environment has a version that modifies the production environment. There is a hit involved in the call to file_exists, but PHP caches it so the effect is minimal. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Interpotential.com Phone: +31615397471 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Database includes
I'm curious as to how everyone organises and includes their classes in PHP5. Let's take a simple example that has 3 classes: customer, order, and database. The database class has a base sql db class (I know there is PDO and other things but this class is already written and working) and classes that inherit from the base class for dev, test, and prod, passing the associated logins. The customer and order will both use the appropriate database class depending on which environment its in (e.g., SalesDevDB, SalesTestDB, SalesProdDB). I don't want to have to go into the customer and order class code and change which db class it uses when I move it from dev to test and from test to prod. What's the proper way to handle this? Or am I way off base? Thanks. Regards, Bruce -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Database includes
On Sunday 26 August 2007, Bruce Cowin wrote: I'm curious as to how everyone organises and includes their classes in PHP5. Let's take a simple example that has 3 classes: customer, order, and database. The database class has a base sql db class (I know there is PDO and other things but this class is already written and working) and classes that inherit from the base class for dev, test, and prod, passing the associated logins. The customer and order will both use the appropriate database class depending on which environment its in (e.g., SalesDevDB, SalesTestDB, SalesProdDB). I don't want to have to go into the customer and order class code and change which db class it uses when I move it from dev to test and from test to prod. What's the proper way to handle this? Or am I way off base? The best way to handle that is to not use 3 separate classes. You should have one and only one code base that works on all installations. Then have a config file of some sort in which you specify your DB credentials. There's a variety of ways to do that (ini file, a PHP file with a database url, a PHP file that just has a couple of variables in it, or constants instead, etc.). Pick one you like. Then have your DB connection class read that file's data one way or another and connect as appropriate. If you have to modify anything other than a single config file in order to move your site/app from one server to another, then you have a design flaw. (I'd say that applies for moving the site to a subdirectory on a server too, but that takes a bit more effort.) Cheers. -- Larry Garfield AIM: LOLG42 [EMAIL PROTECTED] ICQ: 6817012 If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it. -- Thomas Jefferson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Database includes
Bruce Cowin wrote: I'm curious as to how everyone organises and includes their classes in PHP5. Let's take a simple example that has 3 classes: customer, order, and database. The database class has a base sql db class (I know there is PDO and other things but this class is already written and working) and classes that inherit from the base class for dev, test, and prod, passing the associated logins. The customer and order will both use the appropriate database class depending on which environment its in (e.g., SalesDevDB, SalesTestDB, SalesProdDB). I don't want to have to go into the customer and order class code and change which db class it uses when I move it from dev to test and from test to prod. What's the proper way to handle this? Or am I way off base? Could you just have 3 config files and use the appropriate one in each environment? The settings are in 'config.php'. When it's in dev: cp dev.php config.php when it's in test: cp test.php config.php :) -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Database includes
On Sun, August 26, 2007 9:16 pm, Bruce Cowin wrote: I'm curious as to how everyone organises and includes their classes in PHP5. Let's take a simple example that has 3 classes: customer, order, and database. The database class has a base sql db class (I know there is PDO and other things but this class is already written and working) and classes that inherit from the base class for dev, test, and prod, passing the associated logins. The customer and order will both use the appropriate database class depending on which environment its in (e.g., SalesDevDB, SalesTestDB, SalesProdDB). I personally wouldn't make up a whole new class just to pass in a different username/password/host/database... I don't want to have to go into the customer and order class code and change which db class it uses when I move it from dev to test and from test to prod. What's the proper way to handle this? Or am I way off base? Once you don't have sub-classes for the 3 environments, you don't change anything in the customer nor order class. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php