Anular $argv para getopt

ejemplo.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);

Finalmente getopt does not look up to $GLOBALS para obtener argv. So it there any way I can override argv matriz?

preguntado el 28 de mayo de 14 a las 13:05

I don't think its possible to change the arguments that getopt() esta usando. -

1 Respuestas

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

curso en línea

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 no 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']);

Esto resultará en algo como:

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

Internamente

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 implementación:

/* 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 - cómo 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.

Respondido el 20 de junio de 20 a las 10:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.