Reputation: 12044
I have a sql.php config file:
$GLOBALS["hostname_con1"] = 'localhost';
$GLOBALS["database_con1"] = 'mydatabase';
$GLOBALS["username_con1"] = 'root';
$GLOBALS["password_con1"] = '';
and I have a Config.php file
class Config{
public static $dbname = ''; // Your database name
public static $dbuser = ''; // Your database username
public static $dbpass = ''; // // Your database password
public static $dbhost = ''; // Your database host, 'localhost' is default.
... many other variables...
}
This class.config is ONLY filled with static variables.
My Goal is to use the sql.php inside the Config.php to avoid redeclare twice.
I tried:
1) "include": gives an error !
2) public static $dbname = $GLOBALS["hostname_con1"]; error
3) I tried a constructor: but constructor is never called since Config.php is never instantiated.
What is the solution to use sql.php inside Config.php ?
Upvotes: 0
Views: 30
Reputation: 42915
There are many alternatives for this, but since you ask for a "static class" I suggest this:
class DbConfig{
const NAME = 'mydatabase';
const USER = 'root';
const PASS = '';
const HOST = 'localhost';
}
Now you can access the credentials in a static manner wherever you need to:
DbConfig::NAME
DbConfig::USER
DbConfig::PASS
DbConfig::HOST
In addition, what I like to do since it makes me feel better (note: this does not really add better security, it just looks like): I include the file declaring this static class by means of auto prepend in my http servers host configuration. That way no one sees where the file is located.
Oh, and by the way: you really should not use the root account for normal database operations. And especially you should not leave it configured without a password...
Upvotes: 3