Is there a way to set the scope of require_once() explicitly to global?
Asked Answered
C

8

12

I'm looking for a way to set the scope of require_once() to the global scope, when require_once() is used inside a function. Something like the following code should work:

file `foo.php':

<?php

$foo = 42;

actual code:

<?php

function includeFooFile() {
    require_once("foo.php"); // scope of "foo.php" will be the function scope
}

$foo = 23;

includeFooFile();
echo($foo."\n"); // will print 23, but I want it to print 42.

Is there a way to explicitly set the scope of require_once()? Is there a nice workaround?

Comedietta answered 23/1, 2012 at 15:2 Comment(9)
The scope of require_once is explicitly set where you define the use of it.Heida
I can think of a horrible work around, if you want that...Penates
There isn't. You need to explictly list the variables to be aliased into the global scope. Either in the function or atop your include script.Squeegee
@DaveRandom: Probably you should add it as an answer ^^Heida
Why are you wrapping a require_once() in a function?Medullated
@afuzzyllama: The above code is just an example for my question. In my application I have a function in which I want to load a dynamically computed list of source files.Comedietta
@tampis: You are aware that require_once will be of limited use as it will define the variables only once?Heida
@hake: Yes, I'm aware of it... Thanks for your hint^^.Comedietta
possible duplicate of run function block in context of global namespace in PHPBrom
B
1

You can use this hacky function I wrote:

/**
 * Extracts all global variables as references and includes the file.
 * Useful for including legacy plugins.
 *
 * @param string $__filename__ File to include
 * @param array  $__vars__     Extra variables to extract into local scope
 * @throws Exception
 * @return void
 */
function GlobalInclude($__filename__, &$__vars__ = null) {
    if(!is_file($__filename__)) throw new Exception('File ' . $__filename__ . ' does not exist');
    extract($GLOBALS, EXTR_REFS | EXTR_SKIP);
    if($__vars__ !== null) extract($__vars__, EXTR_REFS);
    unset($__vars__);
    include $__filename__;
    unset($__filename__);
    foreach(array_diff_key(get_defined_vars(), $GLOBALS) as $key => $val) {
        $GLOBALS[$key] = $val;
    }
}

It moves any newly defined vars back into global space when the include file returns. There's a caveat that if the included file includes another file, it won't be able to access any variables defined in the parent file via $GLOBALS because they haven't been globalized yet.

Ballinger answered 13/5, 2015 at 21:57 Comment(0)
O
5

Apart from "globalizing" your variable, there is no way to do this:

global $foo;
$foo = 42;

OR

$GLOBALS['foo'] = 42;

Then your value should be 42 when you print it out.

UPDATE

Regarding the inclusion of classes or functions, note that all functions and classes are always considered global unless we are talking about a class method. At that point, the method in a class is only available from the class definition itself and not as a global function.

Oeflein answered 23/1, 2012 at 15:7 Comment(2)
I just tried your answer and it worked fine. There should not be any problems, if file foo.php contains functions or classes, right?Comedietta
Thx Mathieu. Your solution works just fine for my application.Comedietta
C
3

You will need to declare global in your foo.php:

<?php
 global $foo;
 $foo = 42;
?>

Otherwise it's probably not possible.

You could try to play around with extract(), get_defined_vars(), global and $GLOBALS in various combinations maybe... like iterating through all defined variables and calling global on them before requiring a file...

$vars = get_defined_vars();
foreach($vars as $varname => $value)
{
  global $$varname; //$$ is no mistake here
}
require...

But i'm not quite sure if you get to where you want to go...

Chicane answered 23/1, 2012 at 15:11 Comment(1)
Ah btw... did i mention this is somewhat horrible, everything in here should only be used as wrong examples how to NOT do things... there's probably a very nice solution for the basic problem you are trying to solve, without horribly misuse of global namespace ;)Chicane
P
3

This is definitely not a "nice" work around but it would work:

function includeFooFile() {
  require_once("foo.php");
  foreach (get_defined_vars() as $key => $value) {
    // Ignore superglobals
    if (!in_array($key, array('GLOBALS','_SERVER','_GET','_POST','_FILES','_COOKIE','_SESSION','_REQUEST','_ENV'))) {
      $GLOBALS[$key] = $value;
    }
  }
}

However, your included file cannot define any functions or classes (and possibly some other things as well that I cannot currently think of) because it will result in a parse error, since you cannot nest classes or functions.

EDIT apparently you can include functions in your file. I had always thought you couldn't but after testing it seems that you can.

Penates answered 23/1, 2012 at 15:15 Comment(7)
This seems to be a working workaround for my problem. I'm now going to try it out...Comedietta
@DaveRandom: This does not support aliasing and superglobals have already been overwritten, so there is no need to check for those theoretically. Practically, get_defined_vars() does not return those anyway in local function scope.Heida
@Heida Indeed after testing properly (5.2.17/win32) it does not, when I checked it for the purposes of this answer I did it in the global scope, where they are returned. I don't understand why they would not be present though - by definition, superglobals are defined in every scope. I am going to leave this answer as it is in case this behaviour changes in a future version. I would rather check and not overwrite superglobals in case there is something I have not foreseen (especially $GLOBALS) - as you say they would already be overwritten so it should make little practical difference.Penates
@Heida you are right (also thought in the first time, that get_defined_vars() will return all accessible vars in the current scope). In PHP 5.3.3 it just return variables in the function scope...Comedietta
If the file cannot define functions, it's almost useless - because that's why you usually include themTwobyfour
@Twobyfour indeed this is largely true (although includes are also useful for HTML template files) but it would seem I was misinformed about this anyway. I was under the impression that defining a function within a function would result in a parse error, but after testing/reading the manual properly I find I was incorrect about this.Penates
@Tomas: The required file can define functions.Heida
H
2

As the scope is explicitly defined where you use require and the like, you would need to specify what to do with the variables inside the scope of the function:

function includeFooFile() {
    require_once("foo.php"); // scope of "foo.php" will be the function scope

    foreach (get_defined_vars() as $k => $v)
    {
        $GLOBALS[$k] = &$v;
    }
}

This example takes care of both, variables and references which might be what you're looking for. Demo. Please note that require_once would only work once and would only define the variables once.

Heida answered 23/1, 2012 at 15:15 Comment(0)
D
1

Pending on your exact requirements, you could use constants. Require your file in the global scope, but inside it set a constant.

IE file.php:

define('MY_CONSTANT', 42);

Then anywhere in your script just use MY_CONSTANT to refer to the value, you won't be able to edit once it's been set though. Other than that, you could globalize your variable as the other answer says, but it's not 100% clear what you're trying to achieve other than simply retrieving a value from the included file? In which case constants should be fine.

Update: Now you've explained that you want an objects properties to be available everywhere, I suggest you look into creating a static class, which once instantiated in your global scope can be used anywhere in your app. Read the linked manual page, it has a bare-bones example.

Dominga answered 23/1, 2012 at 15:7 Comment(2)
I think that the user wants to be able to update an existing value, but if it is just external constants, then your method is the best obviouslyOeflein
In my application $foo will be a object. Objects cannot be stored in constants, right?Comedietta
C
1

I haven't tried it (since using global vars is a bad idea tbh) but this could potentially work:

require_once '...';
$GLOBALS = array_merge($GLOBALS, get_defined_vars());

Alternatively you can just do it manually:

foreach (get_defined_vars() as $k => $v) {
    $GLOBALS[$k] = $v;
}
Crosslet answered 23/1, 2012 at 15:17 Comment(2)
Why do you think using global vars is a bad idea?Comedietta
It's a dogma :) you can google for it, there's more than enough explanations, most fit any language. Basically, even if you need the data to be globally accessible, put it somewhere where it can't interfere with something else. In PHP a static class variable is a good alternative (imho).Crosslet
B
1

You can use this hacky function I wrote:

/**
 * Extracts all global variables as references and includes the file.
 * Useful for including legacy plugins.
 *
 * @param string $__filename__ File to include
 * @param array  $__vars__     Extra variables to extract into local scope
 * @throws Exception
 * @return void
 */
function GlobalInclude($__filename__, &$__vars__ = null) {
    if(!is_file($__filename__)) throw new Exception('File ' . $__filename__ . ' does not exist');
    extract($GLOBALS, EXTR_REFS | EXTR_SKIP);
    if($__vars__ !== null) extract($__vars__, EXTR_REFS);
    unset($__vars__);
    include $__filename__;
    unset($__filename__);
    foreach(array_diff_key(get_defined_vars(), $GLOBALS) as $key => $val) {
        $GLOBALS[$key] = $val;
    }
}

It moves any newly defined vars back into global space when the include file returns. There's a caveat that if the included file includes another file, it won't be able to access any variables defined in the parent file via $GLOBALS because they haven't been globalized yet.

Ballinger answered 13/5, 2015 at 21:57 Comment(0)
O
0

You can use eval() to get your require_once to run in a global context. This method would require changing just two lines in your main program file: your function call, and the return value from the function. No changes to foo.php would be required, or other global iteration tricks.

function includeFooFile() {
    # don't do the require in the fn, let the caller do it
    return 'require_once("foo.php");';
}

$foo = 23;

eval(includeFooFile());
echo($foo."\n"); # will print 42.

There are a couple of catches to the eval() approach though:

Do not include any user-supplied or untrusted strings in the returned & eval'd code: see the eval() documentation as to why. This shouldn't be an issue for require_once's because in general you will be including only known source files.

Your includeFooFile function cannot use for itself any of the variables or functions declared in foo.php, because they do not exist until after the return. If you wanted includeFooFile to manipulate foo.php's changes further, you'd need to define a follow-up function and call that after the eval, or change the return value to include the follow-up code after the require_once. However, the latter approach may get tricky with nested quoting, so a heredoc is recommended. Double-quote style heredocs, shown below allow you to use variables, perhaps parameters passed to includeFooFile. Single-quote heredocs, like <<<'EOINCLUDE', would make quoting much easier if you know for sure you do not require variable interpolation.

function includeFooFile() {
    return
    <<<EOINCLUDE
    require_once('foo.php');
    \$foo++;
    EOINCLUDE;
}

Prints 43.

If you wanted super simple, you could do away with the eval and simply return the name of the file to require_once, and then have the caller just require that:

function includeFooFile() {
    return 'foo.php';
}

$foo=23;

require_once(includeFooFile());
echo($foo."\n"); # will print 42.

But then you cannot add any follow-up code, nor require_once two or more files within includeFooFile. However, you do eliminate the always-uncomfortable eval().

Note: if you were to have to further nest the call to includeFooFile itself inside another function, you would have to create a bubbling-up chain of these evals somehow. What I've demonstrated would not be adequate as is.

Ideally, PHP devs will eventually give us an optional parameter to require_once to specify we want it done in a global scope.

Opener answered 22/12, 2023 at 11:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.