查看 PHP 中的代号和保留名称
这有点奇怪;我认为这实际上是不可能的,但是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
func_get_arg()
而不是使用传统的函数参数:Use
func_get_arg()
instead of using traditional function arguments:您可以借助附加功能来完成此操作。在示例中,我使用 echo 而不是 require:
You can do this with the help of an additional function. In the example I used echo instead of require:
你可以使用这样的东西:
You can use something like this: