I have database credential variables from a file named config.php:
$db_server = 'localhost';
$db_user = 'username';
$db_password = 'secret'
$db_name = 'dbname';
I now have a PHP class in the / class folder, and it works fine for the CRUD process. named MysqlCrud.class.php:
class Database {
private $db_host = 'localhost';
private $db_user = 'username';
private $db_pass = 'secret';
private $db_name = 'dbname';
}
but I want to use centralized variables from config.php. so I add some lines like this:
include('../config.php');
class Database {
global $db_server;
global $db_user;
global $db_password;
global $db_name;
private $db_host = $db_server;
private $db_user = $db_user;
private $db_pass = $db_password;
private $db_name = $db_name;
}
but I got the following error message:
Parse error: syntax error, unexpected 'global' (T_GLOBAL), expecting function (T_FUNCTION) in /home/*** on line **
Why can't I use the variables from the config.php file inside the database class? what did I do wrong? thanks.
source
share