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

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

    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.

提交回复
热议问题