views:

59

answers:

2

I'd like to start with a little background, in case anyone has any better ideas for design.

I'm looking to create a database class that connects to a database at construction. I would use functions to call the database, to get what I want. The database connection would be closed at destruction.

Since I'm connecting in construction, I'd like to use some properties as part of the mysql_connect... but they're not initialized until construction. I was wondering if there is a way to initialize member variables like a member-wise initialization list in C++, but in PHP.

Thanks in advance for any help, all-mighty google wasn't much help in this case.

-Theino

A: 

One option would be to have class constants. I am not sure, if this is exactly what you want, but they maybe close to solving your problem. PHP Class Constants

Gunner
I've looked at class constants, but it looks like I can't make a private constant in PHP! Correct me if I'm wrong, but this is what I've read. I feel wrong making a public constant that is my database password.-Theino
A: 

create a configuration array and pass that to the database class

$config = array(...);
$db = new db( $config );

class db {

    function __construct( array $config ) {
        if ( $config ) {
            $this->db_name = $config['db_name'];
            .....
        }
    }

}

If your other classes need the info they can use the array as well. Basically the registry pattern.

Galen
Very helpful Galen. This looks like the best solution I have found to my problem... and some interesting reading as well!Thanks for your help -Theino