...

View Full Version : Global variables, using external variables as defaults in functions, and existance



krycek
03-21-2003, 04:50 PM
OK.

Firstly, global variables. To use a script-level variable inside a function, I have to declare it as global INSIDE the function. For instance, if my script sets up $myVar, then to see $myVar, I have to say, global $myVar inside the function.

Is there any way to do this from OUTSIDE the function...? Without using the $GLOBALS array? So that that variable will be global for ALL functions without me having to declare it each time...?


Secondly, using external variables as defaults in function. You know that if you DON'T pass a value to a set argument, then you can have a default argument value...? Such as, function myFunc($myVar = "hello"). Passing anything through will use the passed value, otherwise "hello" will be used.

What I want to do, is to have the default value adhere to a variable - however, the function cannot see it! ...kinda like my first question, but maybe the answer is different.


Thirdly, existance. I can check if a variable is in existance by using isset(). But, how can I "see" outside of a function to determine whether or not a variable is set...? If I make it global, it will become set once I do, regardless of whether it is set or not. And, if I use isset() within my function, then of course that variable is not set within my function...


So, these are headaches to me at present. Any ideas and advice would be welcomed :)

::] krycek [::

firepages
03-21-2003, 07:00 PM
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.


<?
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.



<?
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.



<?
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 :)

krycek
03-21-2003, 07:09 PM
hmmmm, kinda what I thought :(

those are good examples but none fill me with great joy :(

ah well... I'll have to get around these things in other ways :) thanks! :thumbsup:

::] krycek [::

firepages
03-21-2003, 07:45 PM
well if you can stick around till PHP5 hits the streets and are happy with OOP ...a lot of complaints about the current OOP inplementation will be addressed along with a few of your issues above , you should be able to define the scope of class variables which may even allow regular functions to access such ? - I have only just started playing with PHP5 OOP and its a bit scary for a non JAVA head like me :)

krycek
03-21-2003, 07:47 PM
heheh, what has it got to do with Java? :p

yeah I played around with PHP 5, trouble is, until it's out I don't want to code for it, for obvious reasons.

Thanks all the same! :)

::] krycek [::



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum