0
votes

I tried to create a connection class in PHP to connect with the DB data, but one error appears on the lines:

const USER = "{$value['user_DB']}";
const PASS = "{$value['password_DB']}";  

Fatal error: Constant expression contains invalid operations in C:\xampp\htdocs\dev\davinci\classes\Conexao_Teste.class.php on line 21

<?php

require_once"../classes/configDB.class.php";

abstract class conection extends configDB{

private $valor;
private $instanceConfigDB;

public function listDB(){
    $valor = $instanceConfigDB->listconfigDB();
    foreach($valor as $value){

        echo "Banco: ID:{$value['id_db']} - Hostname:{$value['hostname']} - DBNAME:{$value['dbname']} - User:{$value['user_DB']} - PASS:{$value['password_DB']}<br>";
    }
}


    const USER = "{$value['user_DB']}";
    const PASS = "{$value['password_DB']}";

    private static $instance = null;

    private static function conect(){
        try {

            if(self:: $instance==null):

                $dsn = "mysql:host={$value['hostname']};dbname={$value['dbname']};charset=utf8";
                self::$instance = new PDO($dsn, self::USER, self::PASS);
                self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            endif;

        } catch (PDOException $e) {
            echo "Erro: " . $e->getMessage();
        }
        return self::$instance;

    }

    protected static function getDB() {
        return self::conect();

    }


}
?>                  
2
you cannot use run time variables for the value of a compile time constant. Not to mention they are undefined in the scope they are in.ArtisticPhoenix

2 Answers

0
votes

You should use

define('USER', "{$value['user_DB']}");

As const would be evaluated at compile time where as define would be evaluated at run time.

0
votes

You are trying to use $value[] outside it's defined scope, and that is why you are getting those errors.

$value[] is declared in method listDB(). If you want to use that value outside of the method you'd have to declare variables outside the method.

//Declaring variables and constants
private $valor;
private $instanceConfigDB;
private $username;
private $pass;
private $hostname;
private $dbname;

public function listDB(){
  $valor = $instanceConfigDB->listconfigDB();
  foreach($valor as $value){

      echo "Banco: ID:{$value['id_db']} - Hostname:{$value['hostname']} - DBNAME: {$value['dbname']} - User:{$value['user_DB']} - PASS:{$value['password_DB']}<br>";

     //Initialize your data
     $username = $value[user_DB];
     $pass = $value[password_DB];
     ....
  }
}

At least this way, your data is then accessible outside your method

Your connect method would then look like this:

private static function conect(){
    try {

        if(self:: $instance==null):

            $dsn = "mysql:host={$hostname};dbname={$dbname};charset=utf8";
            self::$instance = new PDO($dsn, self::$username, self::$pass);
            self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        endif;

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