开发者

Is there an equivalent to a member-wise initialization list in PHP?

开发者 https://www.devze.com 2023-01-17 05:40 出处:网络
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

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 unt开发者_JAVA百科il construction. I was wondering if there is a way to initialize member variables like a member-wise initialization list in C++, but in PHP.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消