BRG BRG - 1 month ago 13
MySQL Question

Syntax error when trying to access a value from static method in global variables

i am facing a problem here where i am trying to get a value in my class from another class static method.

My code is:

class DB{
private static $_instance = null;
private $_pdo,
$_query,
$_results,
$_error = false,
$_count = 0;
$_operators = array('=', '>', '<', '<=', '>=', '!=');
private $database_name = Config::get('mysql/dbname');


private function __construct(){
try{
$database_host = Config::get('mysql/host');
$database_driver = Config::get('database/driver');
$database_username = Config::get('mysql/username');
$database_password = Config::get('mysql/password');
$dns = ''.$database_driver.':host='.$database_host.';dbname='.$this->database_name.'';
$this->_pdo = new PDO($dns, $database_username, $database_password);
$this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->_pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES ".Config::get('database/names')." ");
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_pdo->exec("SET CHARACTER SET '".Config::get('database/charset')."'");
}catch(PDOException $e){
die($e->getMessage());
}
}
}


it gives me this error:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in C:\xampp-php56\htdocs\backend\classes\DB.php on line 46


and line 46 is:
private $database_name = Config::get('mysql/dbname');


any idea why i cannot do that?

Answer

When i put your code in my IDE, I get a syntax error

Expression is not allowed as field default value.

In PHP when creating class attributes you can assign a scalar value but not an expression (anything that has to be evaluated)

This link has more: Class - variable declaration

Solution: (note the semicolon after _error = false became a comma. Per @Edmund Dantes) Assign the value in the constructor.

class DB{
        private static $_instance = null;
        private $_pdo,
                $_query,
                $_results,
                $_error = false,
                $_count = 0,
                $_operators = array('=', '>', '<', '<=', '>=', '!=');
        private $database_name;


        private function __construct(){
            $this->database_name = Config::get('mysql/dbname');
            try{
                $database_host = Config::get('mysql/host');
                $database_driver = Config::get('database/driver');
                $database_username = Config::get('mysql/username');
                $database_password = Config::get('mysql/password');
                $dns = ''.$database_driver.':host='.$database_host.';dbname='.$this->database_name.'';
                $this->_pdo = new PDO($dns, $database_username, $database_password);
                $this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
                $this->_pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES ".Config::get('database/names')." ");
                $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $this->_pdo->exec("SET CHARACTER SET '".Config::get('database/charset')."'");
            }catch(PDOException $e){
                die($e->getMessage());
            }
        }
}