What is? The config.php file and db.php In the config.php file there is an array with parameters for connecting to the database $ db ['host'] = 'localhost'; $ db ['user'] = 'zverev earring'; and so on.

In the file db.php there is a class for working with the database

Attention! Question! How do I transfer the value of the $ db array to this class? Thanks in advance!

  • Make $ db global ^^ - Nikolai Kim
  • and without globality in any way nizya? - vinnie
  • 2
    You can pass to the class constructor when creating. What is the problem? - Nikolai Kim
  • better organize the config not as $db['user'] but as $config['db']['user'] - zb '16
  • one
    And then when declaring a class instance to the constructor, pass an array $ db = new DB ($ config ['db'] ['user']); So? too "huge" turns out - vinnie


1 answer 1

Approximate class db

 class DB { protected $_username; protected $_password; protected $_host; protected $_database; function __construct($dbConfig) { $this->_username = $dbConfig['username']; $this->_password = $dbConfig['password']; $this->_host = $dbConfig['host']; $this->_database = $dbConfig['database'] } } 

In the file where you need to initialize this class you connect config.php , if it is not already connected.

Well, and create an object: $ db = new DB ($ configDb);

  • Thank!!!!!!!! - vinnie