Evert Lammerts wrote:
My system is accessible through an index.php file. This file does a bunch of includes to include global variables, which in function context become available through the global keyword:

index.php

<?php
require_once "global_vars.php";

function f() {
   global $global_var;
   var_dump($global_var);
}

f();

?>

However, apart from access through the index.php file I want to be able to include my index.php file from a function context:

<?php

   function includeIndex() {
      require_once ("index.php");
   }

?>

The logical result is that variables that are globally accessible to functions when the system is accessed through index.php are now in the context of includeIndex() - so they're not global.

I want to fix this problem by not having the include inside of the function context. How to go upon this?

In index.php rather than declaring vars like so...

$var = 'value';

...declare them in the $GLOBALS array like so...

$GLOBALS['var'] = 'value';

$var is then in the global scope regardless of where it was set.

-Stut

--
http://stut.net/

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

Reply via email to