I think the issue is that these are global variables used inside a function or class scope.
If you want to access these from within a function scope you could use the "global" keyword, for example:
public function foo() {
global $user;
echo $user;
}
However – using global (and global variables in general) is usually considered pretty smelly these days.
Some alternatives:
Pass the configuration into the constructor of your class, therefore:
$config = array(
'user' => 'adam',
'pass' => 'something'
);
class MyConsumer {
private $config;
public function __construct($config) {
$this->config = $config;
}
}
$instance = new MyConsumer($config);
This is usually termed "dependency injection", because your config can be easily substituted for something else depending on the context (such as in unit tests).
An alternative is to have a singleton class which encapsulates your config, which you'd access something like (not tested but you get the idea):
// production_config.php
return array(
'user' => 'adam',
'password' => 'something'
);
// config.php
class Config {
protected $config;
protected static $instance = null;
protected function __construct() {
$this->config = require('production_config.php');
}
protected function getKey($key) {
if(isset($this->config[$key])) {
return $this->config[$key];
}
// Possibly throw exception or something else if not found
}
public static function get($key) {
if(self::$instance === null) {
self::$instance = new self();
}
return self::$instance->getKey($key);
}
}
So you can go:
$user = Config::get('user');
This is perhaps easier than passing your config into every class, but it's more difficult to substitute your config for testing purposes.