Class connection properties

What is the best way to define connection properties for a database in a class

I have a class which connects to a database. In that class I have my public properties defined and use them within my connect method (actually using my constructor for this).

I have seen examples where these values are passed in when the object is created or are held in a config file and included. I have read that defining these properties in the class itself makes the class less abstract and extendible

What is the best way?

If you mean DB connection parameters like login, password, database name used, host and port then I prefer to define them in a separate configuration file (.php file of course) placed in a folder not accessible from the web directly (as well as other .php files intended for inclusion).

Having a separate .php configuration file is a quite common thing. E.g. this is how it is done in OSCommerce, X-Cart and many other known solutions.

This helps to configure the system pretty quickly if configuration is ever done not by you but by some other programmer.

so with this setup …

include/class.php

application.php

configuration.php

my configuration.php holds the parameters and i include this into my application.php

I now have access to my parameter variables within application.php so do i pass these in as arguments to my class on my connection method call


// how you deal with configuration is a whole story on its own
$config['dsn'] = 'mysql:host=localhost;dbname=yourdb';
$config['username'] = 'root';
$config['password'] = 'secret';

// but look! a database instance - why do you need to wrap it?
$db = new PDO($config['dsn'], $config['username'], $config['password'])

thanks guys