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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。