Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argv and argc are undefined or null

When i try this in __construct:

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

It returns error

Undefined variable: argc and
Undefined variable: argv

and array

(size=0) empty

When i declare $argc and $argv in global it returns all null.

Also i am parsing arguments using nncron like this:

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

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

What is solution?

like image 484
user1814358 Avatar asked Mar 16 '23 17:03

user1814358


1 Answers

$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();

Will provide:

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

when called like this php a.php 123

Update: Class example

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

$foo = new Foo($argv);
like image 87
joconja Avatar answered Mar 24 '23 13:03

joconja