views:

840

answers:

5

Let's assume we have the following structure:

index.php
config.inc.php
\ lib
\ lib \ common.php

Several parameters like database name, user & co are configured in config.inc.php. What is the proper way to access them i.e. from a function located in \lib\common.php. Do I really have to do an include_once("config.inc.php") within each function there?

It doesn't seem to work if:

  • config.inc.php is included once once in index.php, before including \lib\common.php there
  • if config.inc.php defines all variables before including \lib\common.php and all other files (this way I would only have to include config.inc.php in all "central" files at the level of index.php
  • neither does it work if config.inc.php is included at the top of \lib\common.php

Many thanks - I wasn't able to find a solution with Google!

Solution

I included config.inc.php once in index.php (as suggested by Galen) and used global (as suggested by David). Everything is working as I would expect it, many thanks!

Later on I will definitely look into auto_prepend as suggested by n3rd, tkx for that as well!

+2  A: 

You simply do:

include_once("..\\config.inc.php");

at the top of common.php.

Now there are a few things here - first the backslashes are annoying, and you can (even on windows, exchange them for forward slashes ("../config.inc.php"). If you have the directory where config.inc.php is contained within in your include path, you can even just do "config.inc.php".

Last and not least, if the data in config.inc.php is required for common.php to work, i suggest you switch to require_once() instead, as that will cause a call to exit() or die() in case the file fails to be included, hence stopping further execution.

EDIT: Ah I didn't spot what others said. To use variables that are declared outside of a function inside of a function, you need to let the function know that it needs to "pull" these variables inside the function scope by using the global keyword (as other have said).

Consider the following example:

$var = "Hello World";
function changeVar(){
    $var = "Bye World!";
    echo $var . "\n";
}
changeVar();
echo $var;

The output of above stated code, is NOT:

Bye World!
Bye World!

but rather:

Bye World!
Hello World

This is due to the fact that the $var INSIDE of the function is its own variable, different from the $var defined OUTSIDE of the function. Change this to:

$var = "Hello World";
function changeVar(){
    global $var;

    $var = "Bye World!";
    echo $var . "\n";
}
changeVar();
echo $var;

And now you have the expected output:

Bye World!
Bye World!
kastermester
+3  A: 

You have to use the global keyword to access variables which were defined outside of a function:

$var1 = "muh";
$var2 = "foo";

function test() {
    global $var1;
    echo "var1=$var1; var2=$var2\n";
}

will print "var1=muh; var2=".

David Schmitt
A: 

U use the auto_prepend feature to do exactly that. The prepended file then searches up the directory hierarchy from the current script and looks for files ending with '.autoinc.php' and includes them in reverse order, i.e. the ones in subdirectories can overwrite stuff defined in files further up the hierarchy. This is set up once and works automatically everywhere and is entirely unobtrusive. I find this to be a rather beautiful and universal solution.

n3rd
+1  A: 

If you include config.inc.php in the index.php file...all files that are included after it will be able to use the data inside it.

If you have functions in common.php you'll have to use the global keyword, or pass the data as arguments, in order to access the data in config.inc.php

Galen
A: 

IMHO that's not a good practice to define variables in configurational purposes. You can rewrite them inside your code. Better define constants. As a benefit - they are in global scope and you can use them from anywhere without "global" keyword.

I usually use an abstract Config class with static methods, which parses external data (I use XML config files outside the DOCUMENT_ROOT) and then provides it, casted to objects, with code like this

$dbalias = Config::get('db');
DB::connect($dbalias);
echo $dbalias->user . '@' . $dbalias->host;

It is very useful when you work with team and use versioning tool (like SVN). Evereyone in your team work on localhost an commit everything but local config file which is not being versioned.

Not sure, that this way is the best, but safe and very comfortable to code, to read and to support.

Jet