Re: Database includes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
larry@xxxxxxxxxxxxxxxx		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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux