Hi,
I don't if this is possible, but I have a question about the following problem:
I want use the variables in a PHP application conf file like
<?
unset($CFG);
$CFG->dbtype = 'mysql';
$CFG->dbhost = 'localhost';
$CFG->dbname = 'dbname';
$CFG->dbuser = 'username';
$CFG->dbpass = '123456';
?>
Is it possible to pass the above settings to another PHP file so that I don't have to define the host name, user name and password over and over?
Thanks.
Jeffrey
Using variables in a configuration file from another file
Moderator: General Moderators
Re: Using variables in a configuration file from another file
Save that file as "some/random/folder/config.php" and put this in another php file:
It's now just as if the contents of config.php were in the other php.
Code: Select all
require_once("some/random/folder/config.php");Re: Using variables in a configuration file from another file
Thanks. But I tried that and it didn't work. How do I refer to these variables? Do I use $CFG->dbname or $$dbname?Apollo wrote:Save that file as "some/random/folder/config.php" and put this in another php file:It's now just as if the contents of config.php were in the other php.Code: Select all
require_once("some/random/folder/config.php");
Thanks.
Jeffrey
Re: Using variables in a configuration file from another file
Refer to them just like you assign them, e.g. try this:
You're not exactly using the cleanest variable setup btw, since $CFG is not a defined class and you just access random members. I'm not even sure if that works with all PHP versions.
Instead I'd recommend:or
Code: Select all
echo $CFG->dbname;Instead I'd recommend:
Code: Select all
$CFG_dbname = 'dbname';
$CFG_dbuser = 'username';
etcCode: Select all
$CFG = array();
$CFG['dbname'] = 'dbname';
$CFG['dbuser'] = 'username';
etcRe: Using variables in a configuration file from another file
here is another way...
and just do a
anywhere you need it.
and that's it.
Code: Select all
class mysql_config
{
var $dbtype = ""; //database type
var $dbhost = ""; //database host
var $dbname = ""; //database name
var $dbuser = ""; //database user
var $dbpass = ""; //database login password
function mysql_config()
{
$this->dbtype = 'mysql';
$this->dbhost = 'localhost';
$this->dbname = 'dbname';
$this->dbuser = 'username';
$this->dbpass = '123456';
}
}
Code: Select all
$CFG = new mysql_config();and that's it.
Re: Using variables in a configuration file from another file
maybe not versions trouble, but it will fail when strict type checking is turned on, and there is a very good chance of that happenning, though usually it is not an issue.Apollo wrote:I'm not even sure if that works with all PHP versions.