doudu3961 2011-09-16 08:04
浏览 48
已采纳

如何替换具有未知参数名称的函数?

This is what I want to do:

$var = "foo";  
$unknown = "";  
function do($arg1, $arg2) {  
    $arg2 = "bar";
    magic($arg2);
    }  
do($var, $unknown);  
echo $unknown;

This should return: bar

I know that this would work if I'd define my function like this:

function do($arg1, $unknown) {
    global $unknown;
    $unknown = "bar";
}

As you probably guessed already, the variable name of $unknown is unknown.

I need this because I'm writing a PHP sandbox using apd. I want to replace exec($cmd, $ret) in a PHP script I don't known with my own function.

So if the unknown PHP script looks like this:

$foo = "";
exec('whois google.com', $foo);
echo $foo;

I'll have a problem replacing exec with my own function do() emulating the same functionality.

  • 写回答

1条回答 默认 最新

  • dongyi6269 2011-09-16 08:07
    关注

    You want to pass the parameter by reference:

    $var = "foo";  
    $unknown = "";  
    function foo($arg1, &$arg2) {    // NOTE EXTRA "&"
        $arg2 = "bar";
    }  
    foo($var, $unknown);  
    echo $unknown;
    

    See it in action.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题