duanque19820925 2014-05-28 12:42 采纳率: 0%
浏览 44
已采纳

为getopt覆盖$ argv

example.php:

$args = __FILE__.' -vvv';
$argv = explode(' ', $args);
$argc = count($argv);
$GLOBALS['argv'] = $_SERVER['argv'] = $argv;
$GLOBALS['argc'] = $_SERVER['argc'] = $argc;

var_export(getopt('v'));

$ example.php -v

> array('v' => false);

Eventually getopt does not look up to $GLOBALS to get argv. So it there any way I can override argv array?

  • 写回答

1条回答 默认 最新

  • dongzhong5967 2014-05-28 13:36
    关注

    TL;DR

    No, there is no native way to do this. Depending of your goals there may be other ways to resolve the issue, but overriding is not one of them.


    Operating on super-globals

    Structure

    To realize why it is so, you should know, that super-globals are not just "variables" to which you are referring. That means, if you are using $argv to de-reference arguments list, it does not mean that you'll access some data which is stored in "variable" $argv. Instead, you'll access to data container by "link", called $argv. However, there are different ways to access this data - $_SERVER['argv'] or $GLOBALS array at least. To illustrate, I'll go with the code:

    var_dump($argv, $_SERVER['argv']);
    unset($argv);
    var_dump($argv, $_SERVER['argv']);
    

    This will result in something like:

    array(2) {
      [0]=>
      string(11) "example.php"
      [1]=>
      string(4) "-vvv"
    }//<--------------------- derived from $argv
    array(2) {
      [0]=>
      string(11) "example.php"
      [1]=>
      string(4) "-vvv"
    }//<--------------------- derived from $_SERVER['argv']
    NULL//<------------------ we've unset $argv, so unset a reference
    array(2) {
      [0]=>
      string(11) "example.php"
      [1]=>
      string(4) "-vvv"
    }//<--------------------- but data is still there and available via another reference
    

    Internally

    As you can see, the reference can be destroyed, but actual data will remain untouched. That will be stored in symbols table. Many PHP functions are accessing this structure to retrieve data, and getopt() is not an exception. So the conclusion is - yes, you can modify the reference (or even destroy it), but actual data will be still in super-globals.


    getopt()

    Now, about this function. Just take a look at its implementation:

    /* Get argv from the global symbol table. We calculate argc ourselves
     * in order to be on the safe side, even though it is also available
     * from the symbol table. */
    if (PG(http_globals)[TRACK_VARS_SERVER] &&
        (zend_hash_find(HASH_OF(PG(http_globals)[TRACK_VARS_SERVER]), "argv", sizeof("argv"), (void **) &args) != FAILURE ||
        zend_hash_find(&EG(symbol_table), "argv", sizeof("argv"), (void **) &args) != FAILURE) && Z_TYPE_PP(args) == IS_ARRAY
    ) 
    {
        //...omitted
    }
    

    It's clearly stated, that it will try to search "argv" in symbol_table (if you want - here's a link to - how it will be done exactly). And that means - it will access actual data, thus, overriding it externally will have no effect.

    As a result - getopt() will work with data which were gathered at script startup, and that data will contain actual parameters, no matter if you'll override them inside your script.

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c