在PHP的全局名称空间的上下文中运行功能块 [英] run function block in context of global namespace in PHP

查看:37
本文介绍了在PHP的全局名称空间的上下文中运行功能块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的想法是我想拥有一个需要库的自定义函数.像这样:

So the senario is that I want to have a custom function for requiring libraries. Something like:

define('E_ROOT', str_replace('//','/',dirname(__FILE__)));
/* ... */
function e_load($fn, $allowReloading = FALSE) {
    $inc = E_ROOT.'/path/here/'.$fn.'.php';
    if($allowReloading)
        require $inc; // !!!
    else
        require_once $inc; // !!!
}

问题是requirerequire_once会将文件加载到函数的名称空间中,这对于函数库,类等没有帮助.那么有没有办法做到这一点?

The problem being that require and require_once will load the files into the namespace of the function, which doesn't help for libraries of functions, classes, et cetera. So is there a way to do this?

(最好完全避免使用requirerequire_once,只要它不使用eval,因为它已被许多主机禁止使用.)

(Something avoiding require and require_once altogether is fine, as long as it doesn't use eval since it's banned on so many hosts.)

谢谢!

推荐答案

从技术上讲,include()的作用是要在该点上在PHP中插入包含脚本的文本.因此:

Technically include() is meant to act as though you're inserting the text of included script at that point in your PHP. Thus:

includeMe.php:
<?php
    $test = "Hello, World!";
?>

includeIt.php:
<?php
    include('includeMe.php');
    echo $test;
?>

应与以下内容完全相同:

Should be the exact same as:

<?php
    /* INSERTED FROM includeMe.php */
    $test = "Hello, World!";
    /* END INSERTED PORTION */
    echo $test;
?>

意识到这一点,创建一个动态包含文件的功能的想法与一起拥有动态代码一样有意义(并且很容易做到).有可能,但是会涉及很多元变量.

Realizing this, the idea of making a function for dynamically including files makes about as much sense (and is about as easy to do) as having dynamic code all-together. It's possible, but it will involve a lot of meta-variables.

我会研究PHP中的变量 get_defined_vars 函数,用于将变量引入全局范围.可以使用类似的方法完成此操作:

I'd look into Variable Variables in PHP as well as the get_defined_vars function for bringing variables into the global scope. This could be done with something like:

<?php
define('E_ROOT', str_replace('//','/',dirname(__FILE__)));
/* ... */
function e_load($fn, $allowReloading = FALSE) {

    $prev_defined_vars = get_defined_vars();

    $inc = E_ROOT.'/path/here/'.$fn.'.php';
    if($allowReloading)
        require $inc; // !!!
    else
        require_once $inc; // !!!

    $now_defined_vars = get_defined_vars();

    $new_vars = array_diff($now_defined_vars, $prev_defined_vars);

    for($i = 0; $i < count($new_vars); $i++){
        // Pull new variables into the global scope
        global $$newvars[$i];
    }
}
?>

使用require()require_once()代替e_load()

请注意,函数和常量应该始终在全局范围内,因此无论在何处定义它们,都可以在代码中的任何位置调用它们.

Note that functions and constants should always be in the global scope, so no matter where they are defined they should be callable from anywhere in your code.

一个例外是在类中定义的函数.这些只能在类的名称空间中调用.

The one exception to this is functions defined within a class. These are only callable within the namespace of the class.

我只是自己测试了一下.函数在全局范围内声明.我运行了以下代码:

I just tested this myself. Functions are declared in the global scope. I ran the following code:

<?php
function test(){
    function test2(){
        echo "Test2 was called!";
    }
}

//test2(); <-- failed
test();
test2(); // <-- succeeded this time
?>

因此仅在运行test()之后才定义函数,但随后可以从test()外部调用该函数.因此,通过我之前提供的脚本,唯一需要进入全局范围的就是变量.

So the function was only defined after test() had been run, but the function was then callable from outside of test(). Therefore the only thing you should need to pull into the global scope are your variables, via the script I provided earlier.

这篇关于在PHP的全局名称空间的上下文中运行功能块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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