Thank you.
The error in PHP Storm that I am getting is:
Undefined variable '$db_user'
Undefined variable '$db_token'
The error is being produced in test.php When I attempt to run it both db_user and db_token are blank. They are not returning any value.
I have tried it a different way here are my files:
classes/variables.php
<?php
return array (
'db_user' => 'test',
'db_token' => 'test'
);
classes/Config.php
<?php
class Config {
private $_config;
public function __construct() {
$this->_config = include('variables.php');
}
public function __get($key) {
if (isset($this->config[$key])) {
return $this->_config[$key];
}
return null;
}
}
classes/connection.php
<?php
class Connection {
private string $server = "mysql:host=localhost;dbname=test";
private array $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,);
protected $con;
public function openConnection($user,$token) {
try {
$this->con = new PDO($this->server, $user, $token ,$this->options);
return $this->con;
}
catch (PDOException $e){
echo "There is some problem in connection: " . $e->getMessage();
}
}
public function closeConnection(): void{
$this->con = null;
}
}
test.php
<?php
require_once "classes/Config.php";
require_once "classes/connection.php";
try {
$config = new Config();
$user = $config->$db_user;
$token = $config->$db_token;
$database = new Connection();
$db = $database->openConnection($user,$token);
echo "Connection Made";
} catch (PDOException $e) {
echo "There is some problem in connection: " . $e->getMessage();
}
echo "<br />Test";