1

I have a config.php file with:

$globalConf = array( 'DbConfig' => array( 'Hostname' => 'localhost', 'Username' => 'myuser', 'Password' => 'xxxxxxxx', 'Database' => 'mydb' ) ); 

and in other file (this includes the config.php) i have this function:

function db_connect() { if (!($db_link = mysqli_connect($globalConf['DbConfig']['Hostname'], $globalConf['DbConfig']['Username'], $globalConf['DbConfig']['Password'], $globalConf['DbConfig']['Database']))) exit(); return $db_link; } 

This doesn't work. I would need to include the global $globalConf inside the db_connect() function, but I don't want to do this (i have more configurations variables and multiple functions that require those and I don't want to use a global $variable in all functions and subfunctions).

I want to set the $globalConf as a persistent global variable (possibly accesible like class?), for example:

mysqli_connect(myglobal::$globalConf['DbConfig']['Hostname']) 

how can do this?

Solved:

class MainConf { public static $globalConf = array( 'DbConfig' => array( 'Hostname' => 'localhost', 'Username' => 'myuser', 'Password' => 'xxxxxxxx', 'Database' => 'mydb' ) ); } 

and:

function db_connect() { if (!($db_link = mysqli_connect(MainConf::$globalConf['DbConfig']['Hostname'], MainConf::$globalConf['DbConfig']['Username'], MainConf::$globalConf['DbConfig']['Password'], MainConf::$globalConf['DbConfig']['Database']))) exit(); return $db_link; } 

2 Answers 2

2

Other than passing the data to the function, which has already been mentioned, you could create a class for your configuration or global data and use a static access method to retrieve them.

<?php class DbConfig { private static $data = array( 'Hostname' => 'localhost', 'Username' => 'username', // ... ); public static function get($name) { return array_key_exists($name, self::$data) ? self::$data[$name] : ''; } } 

Then in your your functions you would access it like so

DbConfig::get('Hostname'); 
Sign up to request clarification or add additional context in comments.

Comments

2

Pass data into function using parameters... for example:

function db_connect($dbinfo) { if (!($db_link = mysqli_connect($dbinfo['Hostname'], $dbinfo['Username'], $dbinfo['Password'], $dbinfo['Database']))) exit(); return $db_link; } 

and call it like

db_connect($globalConf['DbConfig']) 

I think this is the easiest way.

Update

class MainConf { public $db; // this will get your $globalConf['DbConfig'] array function __construct($dbinfo) { $this->db = $dbinfo; // assign it to $db on class construction } function db_connect() { // use it where you want if (!($db_link = mysqli_connect($this->db['Hostname'], $this->db['Username'], $this->db['Password'], $this->db['Database']))) exit(); return $db_link; } } 

Now you can pass your external data into class like this:

$conf = new MainConf($globalConf['DbConfig']); $dblink = $conf->db_function(); 

This way you don't have to define your data inside the class. Just pass it as parameter, assign that param to class variable and use these information everywhere you want inside the class.

But I guess you're using these info only for db_connect() so it's not necessary to keep it on global level inside class. If you call that method manually you can add $dbinfo parameter in db_connect() so you don't need __construct() method and your class will look like this

class MainConf { function db_connect($dbinfo) { if (!($db_link = mysqli_connect($dbinfo['Hostname'], $dbinfo['Username'], $dbinfo['Password'], $dbinfo['Database']))) exit(); return $db_link; } } 

And now call it this way

$conf = new MainConf; $dblink = $conf->db_connect($globalConf['DbConfig']); 

Just choose what the best fits your needs.

2 Comments

i can access a const of a class: My_Class::CONSTONE can i access a variable or array like this?
Outside of class you must pass variable in function using parameters or to set variable as global. From inside the class where (for example) you have defined that array and function, you can access it directly (which is exactly what you did in your sample class). When you have data defined outside of class, again, you have to pass it to the class using __construct() or some other method. As you can see, it's not hard at all. Cheers ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.