require_once 到函数内的全局作用域 [英] require_once to global scope within a function

查看:39
本文介绍了require_once 到函数内的全局作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎如果在函数内调用require_once,则包含的文件不会扩展全局变量范围.如何在函数内require_once一个文件到全局范围?

It seems that if require_once is called within function, the included file doesn't extend the global variable scope. How to require_once a file to global scope from within a function?

我正在尝试做的是一些动态模块加载器:

What I'm trying to do is some dynamic module loader:

function projects_init()
{
        ...
        foreach ($projects as $p) {
                require_once($p['PHPFile']);

                $init_func = $p['init'];
                if ($init_func)
                        $init_func();
        }
}

如果不可能以这种方式使用 require_once,那么最简单的解决方案是什么?(请不要使用繁重的框架.)

If it is not possible to use require_once that way, what is the simplest solution for this? (Please no heavy frameworks.)

它也适用于 PHP 5.2.

it should also work for PHP 5.2.

推荐答案

总结所有信息:

  1. 函数不是问题,它们无论如何都是全局的

  1. functions are not an issue, they will be global anyway this way

对于全局变量,有两个选项:

for global variables, there are 2 options:

  • 在包含的文件中将它们声明为全局的
  • 在该函数中将它们声明为全局(projects_init() 在我的例子中)
  • declare them as global in the included file
  • declare them as global in that function (projects_init() in my case)

这篇关于require_once 到函数内的全局作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆