variables or constants ???
if a 'variable' is actually going to be constant within the script then define it as constant , as a constant it is global in scope and available to all classes and functions in that script (and included scripts)
i.e.
PHP Code:
<?
define('MY_HOST','localhost');
function blah(){
if(defined('MY_HOST')){
echo 'MY_HOST constant = '.MY_HOST;
}else{
echo 'MY_HOST is not defined';
}
}
blah();
?>
as for the external 'defaults' ,again I would look at constants as a backup , though should probably be avoided as its messy.
PHP Code:
<?
define("TYPE",'smelly');
function yak($desc,$type=''){
if(!$type){$type=TYPE;}
echo $type.$desc;
}
yak(' llama');
?>
for number 3 (and see below) - think about using the built in superglobals , its frowned upon but does solve a lot of problems. i.e. you can in advance set aside eg:
$_ENV['MY_VAR']=$whatever;
and since $_ENV is global in scope solves a few of your problems , now real programmers will tell you that you should not mess with the default superglobals but I have yet to find a good reason why other than a few 5 year old books will tell you its wrong..... ok it cant be ported to other languages but is that a likely scenario ?
the other way around all your issues are to think about using clcasses , a'la a structure
ok its still passing globals around at the end of the day, but the only way to avoid this is to do everything in OOP and still you have to pass around references to your different objects.
PHP Code:
<?
class v_structure{
function v_structure(){
$this->MY_HOST='localhost';
$this->MY_USER='username';
$this->MY_PASS='password';
$this->DB='database1';
}
}
$_ENV['MY_VARS']=new v_structure();
class foo{
function foo(){
echo $_ENV['MY_VARS']->MY_HOST;
//OR dereference//
$local=$_ENV['MY_VARS'];
echo $local->MY_HOST;
}
function change_db($db){
$_ENV['MY_VARS']->DB=$db;
}
}
class bar{
function get_db(){
echo $_ENV['MY_VARS']->DB;
}
}
$t=new foo;
$t->change_db('database2');
bar::get_db();
?>
no real answers I know , just some ideas on alternate methods to poke around