0

I'm building a PHP application and I'm not quite sure how to store global configuration settings that can be truely accessed anywhere - it seems that content that is included cannot access other content included from the main, starting page. For example, if index.php includes foo.php and bar.php, bar.php don't have foo.php but index.php has both of them - the included files just can't refer to the other ones. Is this correct?

If yes, how would I set up something like this up?

index.php

<?php
include 'functions/load.php'
echo getHeader();
?>

functions/load.php:

<?php
include_once 'config.php'
include_once 'header.php'
//loads the includes
?>

functions/config.php:

<?php
//I want to store the site URL here.
$siteURL = "http://127.0.0.1";
?>

functions/header.php:

<?php
function getHeader(){
    return "Header for " . $siteURL;
}
?>

How can I set up an config file that can be then accessed anywhere, including inside other included files? Also, is including a file that lists the other includes good practice?

apscience
  • 7,033
  • 11
  • 55
  • 89

3 Answers3

1

It's good practice to include a single bootstrap.php file, which will contain all of the necessary requires. The bootstrap should also define an autoloader, which will be able to autoload classes without having to be explicitly required.

It isn't possible to autoload functions though, so file(s) containing functions will need to be required in bootstrap.php using require_once(). See this question for other alternatives.

Ideally you'd only have a single "entry point" to your application, which is referred to as a "Front Controller". If so, then you'll only need to require your bootstrap.php once at the top of this file.

Edit: Note about configuration

You'd load your configuration within the bootstrap as well. How you do this is up to you...you could just place all your variables directly in bootstrap.php, or else you could require in your configuration from there. Best practice is to store configuration in a separate file.

Configuration via PHP array:

# in /path/to/config.php (not in a publically accessible directory!)
return array(
    'site_url' => '127.0.0.1', 
    'example' => array(
        'animal' => 'cat', 
        'vehicle' => 'car'
    )
)


# in bootstrap.php
$config = require_once('/path/to/config.php');

# anywhere else
$siteUrl = $config['site_url'];
$animal = $config['example']['animal'];

Or you could use an INI file:

# in /path/to/config.ini (not in a publically accessible directory!)
[site]
url = 127.0.0.1

[example]
animal = cat
vehicle = car


# in bootstrap.php
$config = parse_ini_file('/path/to/config.ini', true); 


# anywhere else
$siteUrl = $config['site']['url'];
$animal = $config['example']['animal'];
Community
  • 1
  • 1
RobMasters
  • 4,108
  • 2
  • 27
  • 34
0

You have to include your'config.php' only once. The best way is to put "require_once" to the script, which would be guaranteed included, "index.php", for example. Take a look at MVC pattern.

Alexey Sidorov
  • 846
  • 1
  • 9
  • 17
0

Actually the problem is not with including files. you have wrote the function getHeader() and access the value of the variable from the outside. you can't get the value of the $siteURL from outside of the function. for this you should use the constants:

Try like this:

in config.php

define("SITEURL","http://127.0.0.1");

in header.php

function getHeader(){
    return "Header for " . SITEURL;
}

either you have to pass the value as parameter to the function or use defined constants.

Prasath Albert
  • 1,447
  • 10
  • 20