PHP Extract2 - 如何在当前符号表中分配变量?

发布于 2024-08-10 02:25:49 字数 1505 浏览 3 评论 0原文

我有一个函数,这是比 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 技术交流群。

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

发布评论

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

评论(3

白云不回头 2024-08-17 02:25:50

你应该看看 此评论 @ php doc This我想这就是你需要的

you should take a look at this comment @ php doc This is what you need I think

っ〆星空下的拥抱 2024-08-17 02:25:49

如果这听起来很粗鲁,我很抱歉,但我只是认为没有必要这样做。

为什么,哦为什么,你想要这样做?您尝试做的事情比使用数组本身更费力,而且更烦人。

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.

云之铃。 2024-08-17 02:25:49

让 extract2() 返回一个干净的数组并对其结果运行 extract() 怎么样?

extract(extract2())

我不确定,但我认为 extract() 的功能很神奇并且难以复制。

How about having extract2() return a clean array and run extract() on its result?

extract(extract2())

I don't know for sure, but I think extract()s functionality is magic and difficult to replicate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文