For example, if a. php defines three files in the same directory and the file has been correctly require, an error will occur! C. php outputs constants in a. php and B. php, such as c phpnamespace.
For example, there is a. php definition.
Namespace np1; $ foo = 'foo'; const foofoo = 'foofoo ';
In addition, B. php is defined as follows:
Namespace np2; $ bar = 'bar'; const barbar = 'barbar ';
Defined in c. php
Require "a. php"; require "B. php ";
$ Baz = np1 \ $ foo; $ qux = np2 \ $ bar;
Echo "$ baz and $ qux! "
The three files are in the same directory and are correctly require. an error occurs during running!
C. php outputs constants in a. php and B. php. for example, c. php is as follows:
$ Baz = np1 \ foofoo; $ qux = np2 \ barbar;
Echo "$ baz and $ qux! "
The output is correct.
So the question is, how can I use the variables $ foo and $ bar in a. php and B. php through namespace identification in c. php.
Well, I did not clarify the problem. After trying, echo "$ foo"; can be output! However, since
$ Foo is the variable defined in namespace np1. why cannot I add the namespace name in c. php? for example, echo np1 \ $ foo;
On the contrary, if a. php defines a class and a constant, such as a. php:
Your name: $ name, and your login time: $ time
";} Public function saything () {echo" echoing from function saything of np1 \ C1
";}
}
In the c. php file, you can use the namespace np1 to use the constant foofoo and class C1 in the file a. php, but you cannot use the variable $ foo through the namespace.
For example, c. php
Require "a. php ";
Require "B. php ";
$ Baz = new np1 \ C1 ("leon", time ());
$ Qux = np1 \ foofoo;
$ Baz. saything ();
Echo "$ qux
";
The class C1 and constant foofoo in np1 can be used normally in c. php, but why cannot the variable $ foo in np1 be used through np1 \ $ foo! If you don't believe it, try it.
Imagine if B. php file and. the PHP file has a variable named $ foo, which is displayed in c. php uses $ foo directly without namespace restrictions. how does the program determine which file defines the variable? (I tried Who outputs the variable After require )?