查看 PHP 中的代号和保留名称

发布于 2024-11-26 10:09:01 字数 1641 浏览 3 评论 0原文

这有点奇怪;我认为这实际上是不可能的,但是 SO 社区一次又一次让我感到惊讶;所以就这样吧。

以 PHP 给出;我有以下代码片段:

$path = 'path/to/file.php';
$buffer = call_user_func(function() use($path){
    ob_start();
    require $path;
    return ob_get_clean();
});

包含后, path/to/file.php 将在其范围内包含 $path是否有任何方法可以防止此变量在包含的文件的上下文中可用?

例如,给定 unset() 返回其取消设置的变量的值,我可以这样做:

require unset($path);

但这当然行不通。


对于那些好奇的人,我试图阻止 $path 从 include-er 继承值。

“混淆安全”是我考虑的一个因素;传递类似 $thisIsThePathToTheFileAndNobodyBetterUseThisName 的内容,但这似乎有点愚蠢,而且仍然不是万无一失的。

对于其他应该继承的“保留”变量,我已经使用了extract()unset()

$buffer = call_user_func(function() use($path, $collection){
    extract($collection);
    unset($collection);
    ob_start();
    // ...

编辑:

我的内容最后继续:

$buffer = call_user_func(function() use(&$data, $registry){
    extract($registry, EXTR_SKIP);
    unset($registry);
    ob_start();
    // only $data and anything in $registry (but not $registry) are available
    require func_get_arg(0);
    return ob_get_clean();
}, $viewPath);

也许我的问题有点误导,因为我使用 use() 将变量传递到匿名函数作用域;传递参数是我忽略提及的一个选项。

关于 @hakre 和 use() + func_get_args()

$var = 'foo';
$func = function() use($var){
    var_dump(func_get_args());
};
$func(1, 2, 3);

/* produces
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

This is a bit of a peculiar one; I don't think this is in fact possible, however the SO community has surprised me time and time again; so here goes.

Given in PHP; I have the following snippet:

$path = 'path/to/file.php';
$buffer = call_user_func(function() use($path){
    ob_start();
    require $path;
    return ob_get_clean();
});

When included, path/to/file.php will have $path in it's scope. Is there any way to prevent this variable from being available in the context of the included file?

For instance, given unset() returned the value of the variable it was unsetting, I could do:

require unset($path);

But of course that doesn't work.


For those curious, I'm trying to prevent $path from inheriting a value from the include-er.

"Security-by-obfuscation" is a consideration I made; passing something like $thisIsThePathToTheFileAndNobodyBetterUseThisName, but that seems a bit silly and still isn't foolproof.

For other "reserved" variables that should be inherited, I've already went with extract() and unset():

$buffer = call_user_func(function() use($path, $collection){
    extract($collection);
    unset($collection);
    ob_start();
    // ...

Edit:

What I finally went with:

$buffer = call_user_func(function() use(&$data, $registry){
    extract($registry, EXTR_SKIP);
    unset($registry);
    ob_start();
    // only $data and anything in $registry (but not $registry) are available
    require func_get_arg(0);
    return ob_get_clean();
}, $viewPath);

Perhaps my question was a bit misleading, through my use of use() to pass variables into the anonymous function scope; passing arguments was an option I neglected to mention.

Regarding @hakre and use() + func_get_args():

$var = 'foo';
$func = function() use($var){
    var_dump(func_get_args());
};
$func(1, 2, 3);

/* produces
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

‖放下 2024-12-03 10:09:01

使用 func_get_arg() 而不是使用传统的函数参数:

$buffer = call_user_func(function() {
    ob_start();
    require func_get_arg(0);
    return ob_get_clean();
}, $path);

Use func_get_arg() instead of using traditional function arguments:

$buffer = call_user_func(function() {
    ob_start();
    require func_get_arg(0);
    return ob_get_clean();
}, $path);
追风人 2024-12-03 10:09:01

您可以借助附加功能来完成此操作。在示例中,我使用 echo 而不是 require:

$path = 'hello';

function valStore($value = null) {
    static $s = null;
    if ($value !== null)
        $s = $value;
    else
        return $s;
}

valStore($path);
unset($path); # and gone
echo valStore();

You can do this with the help of an additional function. In the example I used echo instead of require:

$path = 'hello';

function valStore($value = null) {
    static $s = null;
    if ($value !== null)
        $s = $value;
    else
        return $s;
}

valStore($path);
unset($path); # and gone
echo valStore();
柏林苍穹下 2024-12-03 10:09:01

你可以使用这样的东西:

$path = 'path/to/file.php';
function getPath() {
    global $path;
    $p = $path;
    unset($path);
    return $p;
}
$buffer = call_user_func(function() {
    ob_start();
    require getPath();
    return ob_get_clean();
});

You can use something like this:

$path = 'path/to/file.php';
function getPath() {
    global $path;
    $p = $path;
    unset($path);
    return $p;
}
$buffer = call_user_func(function() {
    ob_start();
    require getPath();
    return ob_get_clean();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文