Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$argv in a class

Tags:

php

class

argv

I'm using exec() to exec a file but the file is in a class, I read more about argv but it was confusing. I need to get it to work inside a class.

It says:

Please note that, $argv and $argc need to be declared global, while trying to access within a class method

on php.net

like image 588
user1588878 Avatar asked Aug 12 '12 18:08

user1588878


People also ask

What does argv mean?

What is ARGV? As a concept, ARGV is a convention in programming that goes back (at least) to the C language. It refers to the “argument vector,” which is basically a variable that contains the arguments passed to a program through the command line.

What is $argv in PHP?

$argv — Array of arguments passed to script.

Why is argc always 1?

As you can see, the first argument ( argv[0] ) is the name by which the program was called, in this case gcc . Thus, there will always be at least one argument to a program, and argc will always be at least 1.


1 Answers

That means argc/argv aren't superglobals - they're only visible at the top-level context of PHP scripts, so...

<?php

$x = $argv[1]; // works

class foo {
   function bar() {
       echo $argv[1]; // undefined
   }
   function baz() {
       global $argv;
       echo $argv[1]; // works
   }
}
like image 140
Marc B Avatar answered Oct 01 '22 02:10

Marc B