function get_header() {
global $config;
$header = new Template( 'header' );
$header->set( 'pagetitle', $config['page_title'] );
echo $header->output();
}
Basically, you're using global variable in a local context.
It would be a good idea to encapsulate config in some kind of Config class, with singleton, so the config does not get overwritten by anything.
To be totally compliant with almost good OOP practices ;)
class Config {
protected $data;
public function __construct(array $config) {
$this->data = $config;
}
public function get($key) {
return $this->data['key'];
}
}
class ConfigManager {
public static $configs;
// In "good OOP" this should't be static. ConfigManager instance should be created in some kind of initialisation (bootstrap) process, and passed on to the Controller of some sort
public static function get($configName) {
if(! isset(self::$configs[$configName]))
self::$configs[$configName] = new Config(include('configs/' . $configName. '.php')); // in good OOP this should be moved to some ConfigReader service with checking for file existence etc
return self::$configs[$configName];
}
}
and then in configs/templates.php
return array('page_title' => 'Page Title');
your function would look like this:
function get_header() {
$config = ConfigManager::get('templates');
$header = new Template( 'header' );
$header->set( 'pagetitle', $config->get('page_title') );
echo $header->output();
}
This may seem overly complicated, and of course you don't have to follow this kind of practices, but the more you code, the more you will enjoy good practices.
Using globals is not one of them!