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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化