Namespace-php how to use variables in other files through the namespace

Source: Internet
Author: User
Tags php definition
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 )?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.