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

前端 未结 7 1606
梦谈多话
梦谈多话 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:21

    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;
    }
    

提交回复
热议问题