14

It seems that if require_once is called within function, the included file doesn't extend the global variable scope. How to require_once a file to global scope from within a function?

What I'm trying to do is some dynamic module loader:

function projects_init()
{
        ...
        foreach ($projects as $p) {
                require_once($p['PHPFile']);

                $init_func = $p['init'];
                if ($init_func)
                        $init_func();
        }
}

If it is not possible to use require_once that way, what is the simplest solution for this? (Please no heavy frameworks.)

EDIT: it should also work for PHP 5.2.

Tomas
  • 57,621
  • 49
  • 238
  • 373
  • possible duplicate of [Is there a way to set the scope of require_once() explicitly to global?](http://stackoverflow.com/questions/8973664/is-there-a-way-to-set-the-scope-of-require-once-explicitly-to-global) – Dunhamzzz Jan 25 '12 at 11:56
  • You could either set globals in the included file or have a different way of doing what you want to do, perhaps with autoloaders, reflection etc. You could provide us with some more info and we might be able to think something better. I don't really like using global scope for tasks that could be accomplished differently. – mobius Jan 25 '12 at 11:57
  • ofc you can include files within a function , and anything in that file that is global it will be available globally... – Tudor Jan 25 '12 at 12:01
  • thats how many frameworks do theyr module loading... check codeigniter's load_class function for example – Tudor Jan 25 '12 at 12:03
  • @Dunhamzzz, it's not the exact duplicate, the **linked question states there's no solution and I'm asking for workaround**! – Tomas Jan 25 '12 at 12:07
  • @Tomas look at my answer on that thread, use a static class! – Dunhamzzz Jan 25 '12 at 13:59
  • possible duplicate of [run function block in context of global namespace in PHP](http://stackoverflow.com/questions/3850214/run-function-block-in-context-of-global-namespace-in-php) – outis Jan 26 '12 at 05:25
  • @Tomas: The [linked question](http://stackoverflow.com/questions/8973664/is-there-a-way-to-set-the-scope-of-require-once-explicitly-to-global) states that the scope is defined where the use is. If you need something else, there are numerous answers that show how to deal with global variables, most making use of [`get_defined_vars`](http://php.net/get_defined_vars) (PHP 4.0.4+). Also functions are not an issue. – hakre Jan 26 '12 at 08:59
  • @hakre, *"functions are not an issue"*, this is the answer I was looking for and it was not written anywhere! – Tomas Jan 26 '12 at 09:02
  • @Tomas: Well, a look in the manual would have help you ;) Functions are always in the global namespace as long another namespace isn't given (which is not the case here as you ask for PHP 5.2 which has no namespaces). – hakre Jan 26 '12 at 10:21
  • @Tomas: [Added an answer](http://stackoverflow.com/a/9016702/367456) that makes this more prominent. – hakre Jan 26 '12 at 10:25

4 Answers4

7

To summarize all the information:

  1. functions are not an issue, they will be global anyway this way

  2. for global variables, there are 2 options:

    • declare them as global in the included file
    • declare them as global in that function (projects_init() in my case)
Tomas
  • 57,621
  • 49
  • 238
  • 373
6

The above answer is right, you can use global to get what you need. In the included file just declare the variables global at the beginning of the file, this way the code will run in the function scope but it will change the global variables(yes, you have to be careful and declare everything you need to change as global but it should work), example:

function a() {
     require_once("a.php");
}
a();
echo $globalVariable;

and in the a.php file:

global $globalVariable;
$globalVariable="text";
Alexandru Mincu
  • 780
  • 5
  • 6
  • Sadly the "be careful" doesn't work for me, because I have many global-variables (many of them are only available during software-development), and I can't add the `global`-keyword everytime I need a new variable. Seems I have to include all files I possibly need globally (Instead of choosing one of them dynamically) – maja Jul 29 '13 at 18:45
2

You can use global to put a variable in the global scope.

http://php.net/manual/en/language.variables.scope.php

  • 2
    Eh?? How does this answer the question? What about functions? – Tomas Jan 25 '12 at 11:55
  • 1
    Oops. Good point. I could still call the function inside the other function though. http://codepad.org/JvyvP3fU –  Jan 25 '12 at 12:00
2

Functions are not an issue (ref):

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.

About global variables: As in an existing question regarding the scope of require and the like, the scope is defined where the use is. If you need something else, there are numerous answers (my take) that show how to deal with global variables, most making use of get_defined_vars.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836