Argv和argc未定义或为空 [英] Argv and argc are undefined or null

查看:325
本文介绍了Argv和argc未定义或为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在__construct中尝试此操作时:

When i try this in __construct:

var_dump($argc);
var_dump($argv);
var_dump($_SERVER["argv"]);

返回错误


未定义变量:argc和

未定义变量:argv

Undefined variable: argc and
Undefined variable: argv

和数组


(size = 0)空

(size=0) empty

当我声明$

我也使用nncron像这样解析参数:

Also i am parsing arguments using nncron like this:

* * * * * php \php\class.xmlcontroler.php timeout=60

0 * * * * php \php\class.xmlcontroler.php timeout=3600

什么是解决方案?

推荐答案

$ argv $ argc 仅在全局名称空间。您必须将它们作为构造函数的参数来处理。

$argv and $argc are only available in the global namespace. You would have to handle them as a parameter to your constructor.

<?php
function foo()
{
    var_dump($argv);
}
echo "global\n";
var_dump($argv);

echo "function\n";
foo();

将提供:

global
array(2) {
  [0]=>
  string(5) "a.php"
  [1]=>
  string(3) "123"
}
function
NULL

当这样调用时 php a.php 123

更新:类示例

<?php
class Foo
{
    public function __construct($argv)
    {
        // use $argv
    }
}

$foo = new Foo($argv);

这篇关于Argv和argc未定义或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆