Hmm..

        Funny I was having this same problem yesterday and wanted to post.

        Here's My question, a variable is not actually global is not
actually global until I make it global through "global $make_this_global"
and then I can assess it using $GLOBAL[$make_this_global]. 

        Another method would be to "global"ise it on demand by writing a
little function. (like Rasmus)

I did it like this --> 


---create_globals.php------------
function create_global($passed_variable)
if (isset ($GLOBALS[$passed_variable]))
{
        return $GLOBALS[$passed_variable];
}
---------------end-----------------

--------config.php------------------
$page_title = "Page Title of Web Page"
---------end------------------------

----------index.html----------------
require_once ('create_globals.php');

$local_variable = create_global( 'main_title')
echo "My Page Title is".$local_variable;
--------------end---------------------


Hence This way I can make it global-on-demand. 

Maybe a better way to do it would be to 

---------------config.php-----------
global $page_title = "Alternative Way"
----------------end-----------------

then I can ->echo "This is an ".$page_title; and it'll come out as -> "This
is an alternative way"

The only problem here would be the "EXTRA" typing involved. The other
problem/question is, will there be a speed tradeoff? If I were to global'ise
everything at the 1st place, would it be faster than jumping in and out of
create_global() and going through $page_title & whatever other variables? (I
want to make all global configurations in this file so it'll be easy to
change)

Any help is appreciated.


Cheers,
Mun Heng, Ow
H/M Engineering
Western Digital M'sia 
DID : 03-7870 5168


-----Original Message-----
From: Greg Beaver [mailto:[EMAIL PROTECTED]
Sent: Saturday, July 05, 2003 12:39 AM
To: Rasmus Lerdorf
Cc: Aric Caley; [EMAIL PROTECTED]
Subject: Re: [PHP] include/require inside of function


Hi,

If the file you are including is of your own authorage (I know that 
isn't a word, but whatever :), just refer to global variables always as 
an index of the $GLOBALS array.  This is good practice anyways for any 
file that might be included by another user, for exactly this issue.

I have a file that could be a global include or not in my project, and 
making sure every global variable is referenced as $GLOBALS['varname'] 
fixed it for me without any fancy code (although Rasmus's code is very nice)

Regards,
Greg
--
phpDocumentor
http://www.phpdoc.org

Rasmus Lerdorf wrote:
> On Fri, 4 Jul 2003, Aric Caley wrote:
> 
>>Is there anyway to include a file inside of a function and have the
included
>>stuff be global?  For instance if I defined a class or a function in that
>>include file, I want to be able to use that class outside of the function.
>>
>>On the documentation for include() a poster commented that it did indeed
>>work like this, but my testing indicates it does not.  Everything stays
>>local to the function and goes away when the function ends.
>>
>>Is there a way?
> 
> 
> Functions defined in included files are always global.  So I guess it is
> just the variable you want to put out into the global symbol table.  It's
> a little bit tricky, but you can do it like this:
> 
>     function foo($filename) {
>         extract($GLOBALS, EXTR_REFS);
>         include $filename;
>         $arr = array_diff(get_defined_vars(),$GLOBALS);
>         foreach($arr as $var=>$val) $GLOBALS[$var] = $val;
>     }
> 
> -Rasmus



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

Reply via email to