0

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?

2 Answers 2

1

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());
            }
        }
}
Sign up to request clarification or add additional context in comments.

Comments

1

missing last {

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());
        }
    }
 } <-------- this is missing

1 Comment

of course i have some other functions and it is not the full code only the required portion to see the error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.