尝试从全局变量中的静态方法访问值时出现语法错误

时间:2016-10-14 14:29:52

标签: php mysql

我在这里遇到一个问题,我试图从另一个类静态方法中获取我的类中的值。

我的代码是:

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

它给了我这个错误:

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

和第46行是:private $database_name = Config::get('mysql/dbname');

任何想法为什么我不能这样做?

2 个答案:

答案 0 :(得分:1)

上次遗失{

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 :(得分:1)

当我将代码放入IDE时,出现语法错误

表达式不允许作为字段默认值。

在PHP中创建类属性时,您可以指定标量值,但不能指定表达式(任何必须评估的内容)

此链接有更多: Class - variable declaration

解决方案:(注意_error = false之后的分号变为逗号。每个@Edmund Dantes) 在构造函数中分配值。

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