I'm writing a php script that uses require_once to include my configuration file.
If I wanted to create a new function and wanted to access a variable declared in the require_once file, how would I go about doing this?
One option would be to have the configuration function return an array of configuration values. Then you could either (a) pass that array as a parameter to any function that needs to access those values, (b) assign those values to the $GLOBALS array (where it will be available in all contexts), or (c) declare that array as [man]global[/man] in any function that needs to access it. In terms of keeping your code loosely coupled (which aids both debugging and reusability), (a) would be best and (c) would be worst.
Ok when I said variables, I should have specified I was also referring to classes too... like if I wanted to execute something like this:
function praise_user($v) { $data = array(...); $admin->addUser($data); }
Objects can be passed as function/method parameters, too.
function praise_user($data, $admin) { $admin->addUser($data) } $data = config_function(); $admin = new Admin(); praise_user($data, $admin);