Skip to main content

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


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?


Source: Tips4all

Comments

  1. 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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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...

    ReplyDelete
  5. 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;
    }

    ReplyDelete
  6. 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex