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

前端 未结 7 1602
梦谈多话
梦谈多话 2021-02-19 11:47

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 follow

相关标签:
7条回答
  • 2021-02-19 12:46

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

    0 讨论(0)
提交回复
热议问题