How to access dynamic global information in a static function
I'm setting some global configuration variables in the following manner:
$yaml = file_exists('config.yml') ? Spyc::YAMLLoad('config.yml') : array();
$defaults = array(
'hostname' => 'localhost',
'base_uri' => '/wag/'
);
$config = array_merge($default, $yaml);
Now I'd like to define a function base_url($https)
that returns a base URL. The method body might just be:
return 'http' . ($https ? 's' : '') . '//' . $config['hostname'] . $config['base_uri'];
But I don't know how to access those default variables after they have been created. How would I go about doing so. I'd also be open to finding another way to achieve the end goal I'm looking for (having a configuration variable/constant as well as some utility fu开发者_开发技巧nctions to help me synthesize values based on that configuration data).
Create a config class, define class variables and use get/set methods to store and retrieve the params.
Example:
class Config
{
private $_hostname = "localhost";
private $_baseUri;
public function __construct($_hostname, $baseUri)
{
//initialise vars
}
public function getHostname()
{
return $this->_hostName;
}
public function setHostname($hostName)
{
$this->_hostName = $hostName;
}
}
精彩评论