Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8092945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:23:45+00:00 2026-06-05T20:23:45+00:00

I share the PHP code base across all pages and for each HTTP request

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:23:47+00:00Added an answer on June 5, 2026 at 8:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these links on my site <a rel=nofollow href=http://www.facebook.com/share.php?u=<;url> class=fb_share_button onclick=return fbs_click('http://site.com/profile/4') target=_blank
I have a PHP page and I want to share some data between pages
I have to share an instance of a config object in PHP. This object
Can I share PHP code between two servers? For e.g. say I have a
I want to share my session variables in PHP using many subdomains. I have
I want to use the sharer.php popup to open the share dialog, but would
I'm using php 5.2 with IIS7.5. I have a network share on a NAS
how we do basic debugging in PHP ? Can anybody share true horror story
How to correct instal pear for php. I store all for web server in
I am currently trying to get the code coverage report for a PHP application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.