PHP Framework-PHP namespace usage eliminates the role of include and require

Source: Internet
Author: User
Keywords php php Frame
For example, 2 classes of files

a.phpnamespace A;class Test1{}b.phpnamespace B;class Test2{}

So how do you write it in c.php? is to write

include('a.php');$c = new \A\Test1()

or a direct

use A;$c = new \A\Test1()

Reply content:

For example, 2 classes of files

a.phpnamespace A;class Test1{}b.phpnamespace B;class Test2{}

So how do you write it in c.php? is to write

include('a.php');$c = new \A\Test1()

or a direct

use A;$c = new \A\Test1()

No, continue to include only reduces the likelihood of the XXXX naming conflict in the new xxxxx in different files

namespaces only solve the problem of duplicate names, and you also need to introduce files. Of course, you can also use composer for package management.

Landlord, look at the psr-4 and psr-0.
File loading and namespaces are two different things. Automatic loading is possible without the use of namespaces.
About automatic loading can be seen below:

Spl-autoload-register

They are not a concept at all. Include is the introduction of files, namespaces are encapsulated, classes are categorized
c.php notation:

include('a.php');use A\Test1;$c = new Test1();
  • 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.