Re: Global or include?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jan 5, 2011 at 3:27 PM, Nathan Nobbe <quickshiftin@xxxxxxxxx> wrote:

>  if($cachedConfig !== null)
>  {
>    // load file and store value(s) in $cachedConfig
>  }
>

"No config for you ... one year!"

Sorry, couldn't resist. :p

To expand on Nathan's excellent strategy, you could go one further and add
functions to config.php to return specific values from the config instead of
exposing the raw config object. This will have benefits if you change the
structure of the config later and only have to change your access code in
one place. It also replaces client code such as

    $config = load_config();
    if ($config['facebook']['enabled']) { ... }

with the more readable

    if (isFacebookEnabled()) { ... }

by adding an access function in config.php:

    function isFacebookEnabled() {
        $config = load_config();
        return (bool) $config['facebook']['enabled'];
    }

You would still require_once 'config.php', but now you don't have to expose
knowledge of the config's internal structure to every script that needs to
access it.

David

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux