php - Accessing variables in a file from a class in other file -


if have config.php file variables in so...

config.php:

$cnf['dbhost'] = "0.0.0.0"; $cnf['dbuser'] = "mysqluser"; $cnf['dbpass'] = "mysqlpass"; 

how can access these variables class in file, such as...

inc/db.class.php:

class db() { function connect() { mysql_connect($cnf['dbhost'], $cnf['dbuser'], $cnf['dbpass']); } } $db = new db(); 

so, can use class in file such as...

index.php:

<html> <?php include('config.php'); include('inc/db.class.php'); $db->connect(); ?> </html> 

include config file include, require or require_once @ beginning of db script. need specify $cnf global in function want use, otherwise cannot access global variables:

include "../config.php"; class db() { function connect() { global $cnf; mysql_connect($cnf['dbhost'], $cnf['dbuser'], $cnf['dbpass']); } } $db = new db(); 

edit: on big project prefer use boot.php include php files, wont need include in every file need. having this, need include boot index.php , have disposition definitions. it's slower comfortable.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -