PHP Extract2 - 如何在当前符号表中分配变量?
我有一个函数,这是比 extract() 更安全的提取变量的方法。
基本上,您只需指定要从数组中提取哪些变量名称。
问题是,如何像 extract() 那样将这些变量插入“当前符号表”? (即函数内的局部变量范围)。
我现在只能通过将它们设置为全局变量来做到这一点:
/**
* Just like extract(), except only pulls out vars
* specified in restrictVars to GLOBAL vars.
* Overwrites by default.
* @param arr (array) - Assoc array of vars to extract
* @param restrictVars (str,array) - comma delim string
* or array of variable names to extract
* @param prefix [optional] - prefix each variable name
* with this string
* @examples:
* extract2($data,'username,pswd,name','d');
* //this will produce global variables:
* // $dusename,$dpswd,$dname
*/
function extract2($arr,$restrictVars=null,$prefix=false)
{
if(is_string($restrictVars))
$restrictVars=explode(",",$restrictVars);
foreach ($restrictVars as $rvar) {
if($prefix) $varname="$prefix$rvar";
else $varname=$rvar;
global ${$varname};
${$varname}=$arr[$rvar];
}
}
用法:
extract2($_POST,"username,password,firstname");
echo "Username is $username";
在函数内部工作效果不太好的地方:
function x($data)
{
extract2($data,"some,var,names,here");
//now the variables are are global, so you must:
global $some,$var,$names,$here;
}
知道如何避免全局变量,而是将 var 插入到本地 var 作用域中?
I have a function, which is a safer way to extract variables than extract().
Basically you just specify which variable names are to be pulled out of an array.
The problem is, how do you insert those variables into the "current symbol table" like extract() does? (ie. the local variable scope within a function).
I can only do this by making them global variables for now:
/**
* Just like extract(), except only pulls out vars
* specified in restrictVars to GLOBAL vars.
* Overwrites by default.
* @param arr (array) - Assoc array of vars to extract
* @param restrictVars (str,array) - comma delim string
* or array of variable names to extract
* @param prefix [optional] - prefix each variable name
* with this string
* @examples:
* extract2($data,'username,pswd,name','d');
* //this will produce global variables:
* // $dusename,$dpswd,$dname
*/
function extract2($arr,$restrictVars=null,$prefix=false)
{
if(is_string($restrictVars))
$restrictVars=explode(",",$restrictVars);
foreach ($restrictVars as $rvar) {
if($prefix) $varname="$prefix$rvar";
else $varname=$rvar;
global ${$varname};
${$varname}=$arr[$rvar];
}
}
Usage:
extract2($_POST,"username,password,firstname");
echo "Username is $username";
Where things dont work too well... inside a function:
function x($data)
{
extract2($data,"some,var,names,here");
//now the variables are are global, so you must:
global $some,$var,$names,$here;
}
Any idea how to avoid the global, but instead insert the var into the local var scope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你应该看看 此评论 @ php doc This我想这就是你需要的
you should take a look at this comment @ php doc This is what you need I think
如果这听起来很粗鲁,我很抱歉,但我只是认为没有必要这样做。
为什么,哦为什么,你想要这样做?您尝试做的事情比使用数组本身更费力,而且更烦人。
I'm sorry if this sounds rude, but I just don't see the need for this.
Why, oh why, would you want to do this? What you're attempting to do is more effort and just simply more annoying than to use the array itself.
让 extract2() 返回一个干净的数组并对其结果运行 extract() 怎么样?
我不确定,但我认为 extract() 的功能很神奇并且难以复制。
How about having extract2() return a clean array and run extract() on its result?
I don't know for sure, but I think extract()s functionality is magic and difficult to replicate.