Config.php
class Config {
public static $dbserver = "hostedserverURL";
}
DB.php
require 'Config.php'
class DB {
private $server = Config::$dbserver; // compile-error
private $user = "user";
private $password = "password";
private $database = "databasename";
private $db;
}
compile-error says "syntax error, unexpected '$dbserver', expecting 'identifier' or 'class'"
If I remove the $ and change the line to private $server = Config::dbserver; , the compile-error is gone. but this is not correct.
I get a Runtime error in that case.
Fatal error: Undefined class constant 'Config::dbserver' in ..
So I have to retain the $ , Also as per this SO thread: Fatal error: Undefined class constant
This is where I am making use of it,
public function __construct()
{
$this->db = new PDO(
"mysql:host={$this->server};dbname={$this->database};charset=utf8",
$this->user,
$this->password
);
return $this;
}
Question: How can I refer the static variable dbserver and make that as default value for $server of class DB ? Any ideas please