I have a bit of a problem with variable scope, at the moment. I've read the man pages on scope, and I've found several potential to my problems, but I don't like any of them. I'd like to know whether there are any options other than the ones I'm looking at.
Here's a description of my issue:
I have a script (nav.php) with various functions that I'm using almost in an API-like manner. When a user wants to call the function, he or she includes the nav.php and calls a specific function, which takes care of everything. Nothing is passed during the call, but I do want certain things to be specified by the user. These are loaded from a configuration file (using require_once) which the script (nav.php) loads when it runs.
The problem arises when this configuration file comes into play. Since the variables defined are outside the scope of the functions in nav.php, the functions can't access them. Since the function calls are being made in an external file, there isn't any way to pass the config variables into the function; they're not known to the script which is calling the function.
There are five solutions I've come up with:
The first (and the one I'm currently using) is to make the variables global so they can be access inside the functions. I don't like this, for obvious problems with global variables.
The second is to include the config file in every function. This seems to be horrendously redundant, and I'm having nightmares about performance issues at some later point when a file is called about 50 times by a single instance of a script.
The third solution is to include/require the config file in the file that calls the functions, and use that function call to pass the config to the function. This I don't like because it makes the calling file heavier and increases the amount of technical crap required to implement the call in the first place (something I want to keep as simple as possible).
The fourth solution is to rewrite everything using OOP, but this requires PHP5, which my webhost does not provide (yes, I could change hosts, but I don't feel like doing so atm, nor is it particularly feasible for reasons beyond my control).
The final solution I came up with, again which I don't like, is to put the config variables into a function which is then called by each function in nav.php to return specific configuration settings. I think this solution defeats the purpose of having a config file in the first place, and also makes it less user-friendly (again, my goal is simplicity).
I'm wondering if there are any solutions I've missed, or if use of global variables in this case isn't all that bad (as it seems to be the simplest and most straight-forward approach without recoding in OOP and changing hosts).
Here's what I have currently:
nav.php (Here, I can't have too much code that's not in a function, because it's going to be executed when the page is included and called (below).)
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/navconf.php';
function navpath()
{
global $inline; // Here's that nasty global variable.
...
}
?>
navconf.php (The config obviously holds more than one line. 😉 However, the reason I used a config file is because I don't want users tweaking (and potentially breaking) the actual code.)
<?php
$inline = TRUE;
?>
calling file (As you can see, the call is very simple, just the way I want.)
<?php
include_once ($_SERVER['DOCUMENT_ROOT'].'/nav.php');
navpath();
?>
At this point, I'm more or less content to leave the variable global in scope and be done with it. However, I don't want this to come back and bite me later, so I'd like to find a different solution, if possible. I'm heavily leaning toward an OOP approach with PHP5 and nuking my current webhost, so if that remains the best solution, so be it.