require_once to global scope within a function
Asked Answered
R

4

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.

Rockrose answered 25/1, 2012 at 11:48 Comment(11)
possible duplicate of Is there a way to set the scope of require_once() explicitly to global?Knew
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.Passageway
ofc you can include files within a function , and anything in that file that is global it will be available globally...Zama
thats how many frameworks do theyr module loading... check codeigniter's load_class function for exampleZama
@Dunhamzzz, it's not the exact duplicate, the linked question states there's no solution and I'm asking for workaround!Rockrose
@Rockrose look at my answer on that thread, use a static class!Knew
possible duplicate of run function block in context of global namespace in PHPGennygeno
@Tomas: The linked question 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 (PHP 4.0.4+). Also functions are not an issue.Felucca
@hakre, "functions are not an issue", this is the answer I was looking for and it was not written anywhere!Rockrose
@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).Felucca
@Tomas: Added an answer that makes this more prominent.Felucca
R
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)
Rockrose answered 26/1, 2012 at 9:23 Comment(1)
3. No, it does not seem possible to apply a global scope for require_once from inside a function. (Sadface.)Bridges
M
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";
Milore answered 25/1, 2012 at 12:7 Comment(1)
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)Getup
M
2

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

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

Maribelmaribelle answered 25/1, 2012 at 11:54 Comment(2)
Eh?? How does this answer the question? What about functions?Rockrose
Oops. Good point. I could still call the function inside the other function though. codepad.org/JvyvP3fUMaribelmaribelle
F
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.

Felucca answered 26/1, 2012 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.