• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Really weird "global" bug

#1
[eluser]richthegeek[/eluser]
Getting a really odd global bug..

In my "view":
Code:
print_r( $data );
print_data();

In my misc_helpers file:
Code:
function print_data()
{
   global $data;
   var_dump( $data );
}

products the $data array from the print_r and NULL from the var_dump in the print_data() function.

This also occurs if I put the function codei nthe view file... anyone got an explanation?

#2
[eluser]InsiteFX[/eluser]
Hi,

In my “view”:

Code:
print_r( $data );
print_data($data);

In my misc_helpers file:

function print_data($data)
{
   var_dump( $data );
}
]

No need for using global.

Enjoy
InsiteFX

#3
[eluser]richthegeek[/eluser]
I'm wanting to use a bunch of external vars in it, not just one. It was an example, and i'm too keen on passing them all as vars in a function thats used once every 3 lines...

At any rate, it doesnt explain why global is being fubar

#4
[eluser]Colin Williams[/eluser]
Because views aren't in the global scope. The variables available to the view files are local to the Loader::view method.

#5
[eluser]richthegeek[/eluser]
No doubt that's it whilst it's in a helper, but this problem was also occuring whilst the function code was in the view along with the calls to it:

Code:
function get()
{
   global $var;
   var_dump( $var );
}
var_dump( $var );
get();
produces
Code:
object stdClass ( ... ); NULL
rather than the expected double object output

#6
[eluser]Colin Williams[/eluser]
Well, just because the get() function is local to the Loader::view() method, global still references the very top-level scope, not just a variable in any higher-level scope.

And to get at your title, nothing "weird" is happening here. Whenever in doubt, do a var_dump or print_r on the $GLOBALS superglobal

#7
[eluser]richthegeek[/eluser]
Ah, I was unaware that code in the same file wouldn't necessarily be in the same scope, although it of course makes sense that functions would be assigned to the top-level scope. Found a way around it at any rate by referencing the CI var's loader->vars array.

Thanks for the help, really need to get some sleep -_-


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.