Re: When is a global not a global?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, July 12, 2006 5:52 pm, Nick Wilson wrote:
> After upgrading a CMS, im having a problem with global variables not
> showing up anymore -- configs and things could have changed, but
> search
> as i have, i cannot find anything to help me work out what the problem
> is.
>
> This should work of course:
>
>     $foo = 'bar';
>
>     function foobar() {
>       global $foo;
>       print(" ------ " . $foo);
>       exit;
>     }
>
>     foobar();
>
> It prints *nothing*. Does anyone have an idea as to what might stop
> this
> from functioning as expected?

The only time I have seen this is when what REALLY happens is like this:

<?php
  //The CMS code has something like:
  function cms_include_user_code ($file){
    include $file;
  }
?>

<?php
  //some of your code, pulled in through $file above:
  $foo = 'bar';
?>

<?php
  //More of your code, the $file above, has this:
  function foobar()
    global $foo;
    echo "in foobar, foo is $foo<br />\n";
  }
?>

So, if you work through it, you'll see that your $foo = 'bar'; happens
*INSIDE* the cms_include_user_code function.

So it's not that global isn't working.

It's that you need:

<?php
  global $foo;
  $foo = 'bar';
?>

in that first file, so that your $foo buried inside the CMS function
is global, instead of local to their cms_include_user_code function.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux