|
Posted by Jerry Stuckle on 02/23/06 21:29
Wescotte wrote:
> I'm having an issue where what should be global variables are not in
> scope and I'm confused as to why.
>
> In Case 1 the variables are not in scope for functions in File2
> In Case 2 the varibales are in scope for access in File1's functions
> AFTER the include
> In Case 3 the varibales are in scope for functionsin File2
>
>
> Why does case 1 not work but case 3 does? Does case 2 the proper method
> to use?
>
> CASE 1:
>
> File1:
>
> switch (OBJ) {
> case 1:
> include "file2.php"
> if Function_In_File2()
> ....
> break;
> }
>
> File2:
>
> <?php
>
> $MY_GLOBAL_VARIABLE = "TEST";
>
> Function Function_In_FIle()
> {
> global $MY_GLOBAL_VARIABLE;
> echo $My_GLOBAL_VARIABLE; /* Produces Notice: Undefined variable:
> $My_GLOBAL_VARIABLE in File2.php on line xx */
> }
>
> ?>
>
> CASE 2:
>
>
> File1:
>
> switch (OBJ) {
> case 1:
> include "file2.php"
> if Function_In_File2()
> ....
> break;
> }
>
> File2:
>
> <?php
>
> global $MY_GLOBAL_VARIABLE; $MY_GLOBAL_VARIABLE = "TEST";
>
> Function Function_In_FIle()
> {
> global $MY_GLOBAL_VARIABLE;
> echo $My_GLOBAL_VARIABLE; /* Displays "TEST" */
> }
>
> ?>
>
> CASE 3:
>
>
> File1:
>
> switch (OBJ) {
> case 1:
> include "file2.php"
> echo $MY_GLOBAL_VARAIBLE; /* Displays "TEST" Why does this work and
> not case 1? */
>
> }
>
> File2:
>
> <?php
>
> $MY_GLOBAL_VARIABLE = "TEST";
>
> ?>
>
Case 1 did not work because you did not specify $MY_GLOBAL_VARIABLE as
global in the main part of your code (outside your function
Function_in_file_2).
Case 2 worked because you did define it as global in the main part of
your code also.
Case 3 works because you're not in any function in either case.
The include "file2.php" adds the code in file2.php right where the
include statement is. It's exactly like removing the include statement
and pasting the contents of file2 in file 1 at that point.
So - in your third case, the result is just like:
switch (OBJ) {
case 1:
$MY_GLOBAL_VARIABLE = "TEST"; // From file2.php
echo $MY_GLOBAL_VARIABLE;
}
As to which is the "correct way" - none of the above are good. You
should try to avoid using globals as it ties the code together more.
For instance, what happens if $MY_GLOBAL_VARIABLE gets a bad value in
it? Where was it changed? Being a global, it could be anywhere.
Rather, pass $MY_GLOBAL_VARIABLE as a parameter to the function, i.e.
for case 1:
File1:
switch (OBJ) {
case 1:
include "file2.php"
if Function_In_File2($MY_GLOBAL_VARIABLE)
....
break;
}
File2:
<?php
$MY_GLOBAL_VARIABLE = "TEST";
Function Function_In_File($var)
{
echo $var;
}
?>
I don't like this - it means File1 depends on a value set in File2 which
also ties things together more. But it does solve the global problem.
You could also use a constant, i.e.
define ("GLOBAL_CONSTANT", "TEST");
and use GLOBAL_CONSTANT where you have MY_GLOBAL_VARIABLE, but this only
works if you include one file each run (i.e. don't look and include
multiple files defining GLOBAL_CONSTANT).
Personally, I think I'd look at how I could restructure the files. But
not knowing your code, I can't recommend how to do it.
BTW - the norm in PHP is to use upper case for constants and lower case
(or mixed upper/lower, depending on your style) for variables, i.e.
$my_global_variable or $myGlobalVariable.
I hope this helps.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|