Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-21-2003, 04:50 PM   PM User | #1
krycek
Regular Coder

 
Join Date: Nov 2002
Location: Bristol, UK
Posts: 932
Thanks: 0
Thanked 0 Times in 0 Posts
krycek is an unknown quantity at this point
Global variables, using external variables as defaults in functions, and existance

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 [::
__________________
ithium | SOAPI | SDP | PTPScript manual
"ithium is a non-profit webhost, which is pretty much unique. The mission of ithium is to provide free hosting resources for worthwhile and needy non-profit projects, which otherwise may not be able to obtain such facilities. The money from commercial customers goes to maintain ithium's servers and further development."
krycek is offline   Reply With Quote
Old 03-21-2003, 07:00 PM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,911
Thanks: 5
Thanked 80 Times in 79 Posts
firepages will become famous soon enough
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
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 03-21-2003, 07:09 PM   PM User | #3
krycek
Regular Coder

 
Join Date: Nov 2002
Location: Bristol, UK
Posts: 932
Thanks: 0
Thanked 0 Times in 0 Posts
krycek is an unknown quantity at this point
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!

::] krycek [::
__________________
ithium | SOAPI | SDP | PTPScript manual
"ithium is a non-profit webhost, which is pretty much unique. The mission of ithium is to provide free hosting resources for worthwhile and needy non-profit projects, which otherwise may not be able to obtain such facilities. The money from commercial customers goes to maintain ithium's servers and further development."
krycek is offline   Reply With Quote
Old 03-21-2003, 07:45 PM   PM User | #4
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,911
Thanks: 5
Thanked 80 Times in 79 Posts
firepages will become famous soon enough
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
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 03-21-2003, 07:47 PM   PM User | #5
krycek
Regular Coder

 
Join Date: Nov 2002
Location: Bristol, UK
Posts: 932
Thanks: 0
Thanked 0 Times in 0 Posts
krycek is an unknown quantity at this point
heheh, what has it got to do with Java?

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 [::
__________________
ithium | SOAPI | SDP | PTPScript manual
"ithium is a non-profit webhost, which is pretty much unique. The mission of ithium is to provide free hosting resources for worthwhile and needy non-profit projects, which otherwise may not be able to obtain such facilities. The money from commercial customers goes to maintain ithium's servers and further development."
krycek is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:28 PM.


Advertisement
Log in to turn off these ads.