Why does "Constant expression contains invalid operations" error appear in connection class using PDO in PHP?

0

I have the following object-oriented connection class with PDO:

Conexion.php:

<?php namespace Models;

$archivo = 'config.ini.php';
$configurar = parse_ini_file($archivo, true);

$host= $configurar['basedatos']['host'];
$user = $configurar['basedatos']['user'];
$pass = $configurar['basedatos']['pass'];
$db = $configurar['basedatos']['dbname'];
$charset = $configurar['basedatos']['charset'];

    class Conexion extends PDO
    {
        private $_datos = array(
            "host" => $host,
            "user" => $user,
            "pass" => $pass,
            "dbname" => $db,
            "charset" => $charset
            );

        protected static $_conexion; /* private $_conexion */

        private function __construct() {
            try 
            {
                self::$_conexion = new PDO("mysql:
                    host=$this->_datos['host'];
                    dbname=$this->_datos['dbname'];
                    charset=$this->_datos['charset']", 
                    $this->_datos['user'], 
                    $this->_datos['pass']);

                self::$_conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    echo "Conexion Exitosa"; 
            }

            catch(PDOException $e)
            {
                echo "Conexion Fallida: " . $e->getMessage();
            }
        }

        public function consultaSimple($sql) {
            $this->_conexion->query($sql);
        }

        public function consultaRetorno($sql) {
            $datos= $this->_conexion;
            return $datos;
        }
    }

Config.ini.php:

<?php return; ?> ; 
[basedatos]
driver="mysql"
host="localhost"
port="3306"
user="root"
pass="" 
db="escuelaCRUD"
charset="utf8" 

The error shown is:

  

Fatal error: Constant expression contains invalid operations in   Conexion.php on line 14

    
asked by Victor Alvarado 08.06.2017 в 21:44
source

2 answers

0
  • Modify the position of the code to parse the INI file
  • Add some attributes
  • Add the use PDO, to avoid the global class error, avoid using \ in each PDO

    use PDO;
    class Conexion 
    {
        private $_host;
        private $_user;
        private $_pass;
        private $_dbname;
        private $_charset;
        private $_conexion; /* private $_conexion */
    
        public function __construct() {
            $archivo = 'config.ini.php';
            $configurar = parse_ini_file($archivo, true);
    
            $this->_host = $configurar['basedatos']['host'];
            $this->_user = $configurar['basedatos']['user'];
            $this->_pass = $configurar['basedatos']['pass'];
            $this->_dbname = $configurar['basedatos']['dbname'];
            $this->_charset = $configurar['basedatos']['charset'];
            try 
            {
    
            $this->_conexion = new PDO("mysql:host=$this->_host;
                    dbname=$this->_dbname;
                    charset=$this->_charset", 
                    $this->_user, 
                    $this->_pass);
             $this->_conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    echo "Conexion Exitosa"; 
            }
    
            catch(PDOException $e)
            {
                echo "Conexion Fallida: " . $e->getMessage();
            }
        }
    
        public function consultaSimple($sql) {
            $this->_conexion->query($sql);
        }
    
        public function consultaRetorno($sql) {
            $datos= $this->_conexion;
            return $datos;
        }
    }
    
    $conexion = new conexion();
    
answered by 09.06.2017 / 05:23
source
0

Inside the PDO object, at the time of connection you can not use $ this-> Xvariable, I tell you from experience, it has happened to me, you must do it in the following way:

    private function __construct() {
        try 
        {
            self::$_conexion = new PDO("mysql:
                host=$host;
                dbname=$dbname;
                charset=$charset", 
                $this->_datos['user'], 
                $this->_datos['pass']);

            self::$_conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                echo "Conexion Exitosa"; 
        }
    
answered by 10.06.2017 в 19:25