Reputation: 79359
I share the PHP code base across all pages and for each HTTP request I dynamically require
the "/configs/$site/config.php"
file. The file looks like this:
<?php
$SiteConfiguration = [
'site_title => 'Wiki for Developers',
'mysql_host' => 'localhost',
'mysql_db' => 'wiki-devs',
'articles_per_page' => 10,
/* ... etc ... */
];
?>
The problem I'm facing is that I can't quite access this variable from functions.
For example:
function DisplayArticles() {
echo "Displaying ".$SiteConfiguration['articles_per_page'];
}
It will print just Displaying
and not Displaying 10
.
How can I fix this and have my $SiteConfiguration accessible everywhere? Should I use a class
? What's the best practice here?
Upvotes: 0
Views: 194
Reputation: 46
You can try something like this.
Your "siteConfiguration.php" file:
<?php
$SiteConfiguration = [
'site_title' => 'Wiki for Developers',
'mysql_host' => 'localhost',
'mysql_db' => 'wiki-devs',
'articles_per_page' => 10
];
return $SiteConfiguration;
?>
And this function:
function getConfigVar($var) {
static $config = array();
if( empty($config) ) {
$config = require("siteConfiguration.php");
}
return array_key_exists($var, $config) ? $config[$var] : null;
}
This function can also be modified to handle several configs.
Upvotes: 0
Reputation: 4455
put
global $SiteConfiguration;
in your function, you can find some more info at http://www.php.net/manual/en/language.variables.scope.php
Since you asked for best practice info: (simplest form)
class MySite{
public static function getConfig(){
return array(
'site_title => 'Wiki for Developers',
'mysql_host' => 'localhost',
'mysql_db' => 'wiki-devs',
'articles_per_page' => 10,
/* ... etc ... */
);
}
}
then in your code you can recall it with
$config = MySite::getConfig();
and use it. (obviously with a better, more descriptive name than MySite ;) )
Advantages:
in my opinion it beats globals and it beats passing via arguments since it's cleaner and you control the access to it in all forms. You can make certain attributes readonly/writable via specific getter/setter options, keep count of how many times it's accessed and whatever else you can think of.
Upvotes: 1
Reputation: 17314
function DisplayArticles() {
global $SiteConfiguration;
echo "Displaying ".$SiteConfiguration['articles_per_page'];
}
Edit
You should try to avoid global variable.
A better practice would be to pass your array in parameter
function DisplayArticles( array $config ) {
echo "Displaying ".$config['articles_per_page'];
}
$SiteConfiguration = array( 'site_title' => 'Wiki for Developers',
'mysql_host' => 'localhost',
'mysql_db' => 'wiki-devs',
'articles_per_page' => 10,
/* ... etc ... */
);
DisplayArticles( $SiteConfiguration );
Upvotes: 0
Reputation: 59699
Here's another case where a class for configuration would work great:
class Config {
private static $site_config = array( 'h' => 'Hello', 'w' => 'World');
public static function get( $key) {
return isset( self::$site_config[$key]) ? self::$site_config[$key] : null;
}
}
echo Config::get( 'h') . ' ' . Config::get( 'w');
This will output: Hello World
Upvotes: 0