Assuming that I have 3 PHP files:
-
index.php:
require('config.php'); require('connect_db.php'); new connect_db();
-
config.php:
$config['db_host'] = 'localhost'; $config['db_username'] = 'root'; $config['db_password'] = ''; $config['db_name'] = 'my_db';
-
connect_db.php:
class connect_db{ function __construct(){ $this->conn = new mysqli($config['db_host'], $config['db_username'], $config['db_password'], $config['db_name']); } }
When I run above codes, I meet an error: "Undefined variable: config in...."
.
My question is: How can I use the $config variable inside "connect_db" class without including config.php
file in connect_db.php file.
Thanks!