bodacydo
bodacydo

Reputation: 79359

How to access a global $SiteConfiguration variable in PHP from everywhere?

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

Answers (4)

ex_
ex_

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

Harald Brinkhof
Harald Brinkhof

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:

  1. your php class autoloader will automagically load it for you if setup correctly and your classes can be found, this means not worrying wether you passed a variable, not worrying about function argument placement and not tainting your functions with unnecessary arguments that don't help describe what it does.
  2. you control exactly the access to this data and can make it so that not even your own functions can accidentally change this data, not even when calling other functions that would also need access to it.

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

Luc M
Luc M

Reputation: 17314

use global keyword

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

nickb
nickb

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

Related Questions