Php framework-whether the use of php namespaces saves the role of include and require

Source: Internet
Author: User
For example, if there are two class files {code...}, how can I write them in c. php? Whether to write {code...} or directly {code...}, for example, two class files

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

So how to write it in c. php? Yes write

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

Or directly

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

Reply content:

For example, two class files

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

So how to write it in c. php? Yes write

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

Or directly

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

No. Continuing to include only reduces the possibility of xxxx name conflicts in new xxxxx files.

The namespace only solves the problem of duplicate names and also needs to introduce files. Of course, you can also use composer for package management.

The landlord, look at the psr-4 and psr-0.
File loading and namespace are two different things. It can be automatically loaded without using a namespace.
For more information about automatic loading, see:

Spl-autoload-register

The two are not a concept at all. Include is the introduction file, namespace is encapsulation, Class Classification
C. php:

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.