Is there an equivalent initialization list for members in PHP?

I would like to start with a little background if anyone has any better ideas for design.

I want to create a database class that connects to the database when building. I would use functions to call the database to get what I want. The database connection will be closed upon destruction.

Since I am connecting to the construction, I would like to use some properties as part of mysql_connect ... but they are not initialized until build. I was wondering if there is a way to initialize member variables, such as a member initialization list in C ++, but in PHP.

+3
source share
2 answers

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

class db {

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

}

, . .

+1

- . , , , , , . PHP

0

Source: https://habr.com/ru/post/1765530/


All Articles