Usage basics and examples of PHP namespaces

Source: Internet
Author: User
This article introduces some terms, parsing rules, and application of some advanced functions of PHP namespaces, hoping to help readers use namespaces in projects. The namespace of PHP is one of the most important new features added to PHP 5.3. this concept has been available in C # for a long time, the namespace in php is actually the same as that in c.

I. PHP namespace mainly solves three types of conflicts: constants, functions, and classes.

In general, namespace is equivalent to creating a directory. the code under namespace is put in this directory, which is differentiated from that outside.

/* | --------------------------------- | Namespace example | @ Blackeye poet
 
  
| ------------------------------- */Namespace myself; function var_dump () {echo 100;} var_dump (); // call a custom function (relative path) \ myself \ var_dump (); // call a custom function (absolute path) \ var_dump (100); // call a global function)
 

Note: No code exists before namespace, except declare (). multiple files can use the same namespace, but the content defined under the same namespace cannot conflict. Namespace supports sub-namespaces, such as namespace \ myself \ good, which is equivalent to the concept of multi-level directories.

2. multiple namespaces exist in the same file.

1.

/*** If multiple namespaces are used in the same file, use the formula */namespace nihao \ shijie; function demo (){//.......} namespace hello \ world; function test (){//........} \ nihao \ shijie \ demo (); \ hello \ world \ test ();

2.

/*** If multiple namespaces are used in the same file, we recommend that you expand the braces */namespace nihao \ shijie {function test_one () {//......};} namespace hello \ world {function test_two (){//........}} \ nihao \ shijie \ test_one (); \ hello \ world \ test_two ();

Multiple namespaces are used in the same file. They are mainly used in projects to merge multiple PHP scripts into the same file. In practice, they are not recommended!

III. name resolution rules (several concepts)

1. unrestricted name: the name does not contain a namespace separator, for example, myself.

2. limited name: the name contains namespace delimiters, such as nihao \ shijie.

3. fully qualified name: the name contains a delimiter and starts with a namespace delimiter, for example, \ nihao \ shijie (the concept of absolute path)

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.