Namespace in PHP5.3: Have you used it?

Source: Internet
Author: User
Namespace is the most important change in PHP5.3. C # and Java developers are very familiar with this term, and they can finally better change the structure of PHP applications. Why do we need a namespace? As your PHP code base grows, fix previously defined functions and class names... "> <LINKhref =" http://www.php100.com//statics/styl

 

Namespace is the most important change in PHP 5.3. C # and Java developers are very familiar with this term, and they can finally better change the structure of PHP applications.

Why do we need a namespace?

As your PHP code base grows, there is a higher risk of modifying previously defined functions and class names. when you try to add third-party components or plug-ins, the problem becomes more serious, what if two or more code sets implement a "Database" and "User" class?

Until now, the only solution is to use long class/function names. for example, Wordpress uses the prefix "WP _" before each class and function name _", zend Framework uses descriptive naming conventions, resulting in lengthy class names, such:

Zend_search_paie_analysis_analyzer_common_text_caseinsensitive

Naming conflicts can be solved using namespaces. PHP constants, classes, and functions can be combined into the namespace Library.

How to define a namespace?

By default, all constants, classes, and function names are stored in the global space, just as before PHP supports namespaces.

Use the namespace keyword at the top of the PHP file to define the namespace. it must be the first command (except declare), and no non-PHP code, HTML, or space can appear before it. For example:

 
  1. < ?php  
  2. // Define this code in the 'myproject' namespace
  3. Namespace MyProject;
  4.  
  5. //... Code...

The code below this line is designated to the MyProject namespace. it is impossible to nest the namespace for the same code block or define multiple namespaces. if you do this, only the last namespace can be identified, but you can define different namespace codes in the same file, such:

 
  1. < ?php  
  2. Namespace MyProject1;
  3. // PHP code for the MyProject1 namespace
  4.  
  5. Namespace MyProject2;
  6. // PHP code for the MyProject2 namespace
  7.  
  8. // Alternative syntax
  9. Namespace MyProject3 {
  10. // PHP code for the MyProject3 namespace
  11. }
  12. ?>

Although this can be done, I suggest you do not do this. it is best to define only one namespace in each file to avoid confusion.

Sub-namespace

PHP allows you to define a hierarchical namespace so that the database can be subdivided. the sub-namespace is separated by a backslash character (\), for example:

◆ MyProject \ SubName

◆ MyProject \ Database \ MySQL

◆ CompanyName \ MyProject \ Library \ Common \ Widget1

Call the namespace code

In the lib1.php file, we use the App \ Lib1 namespace to define a constant, a function, and a class, such:

Lib1.php

 
  1. < ?php  
  2. // Application library 1
  3. Namespace App \ Lib1;
  4.  
  5. Const MYCONST = 'app \ Lib1 \ myconst ';
  6.  
  7. Function MyFunction (){
  8. Return _ FUNCTION __;
  9. }
  10.  
  11. Class MyClass {
  12. Static function WhoAmI (){
  13. Return _ METHOD __;
  14. }
  15. }
  16. ?>

Now we can include this code in another php file, such:

Myapp. php

 
  1. < ?php  
  2. Header ('content-type: text/plain ');
  3. Require_once ('lib1. php ');
  4.  
  5. Echo \ App \ Lib1 \ MYCONST. "\ n ";
  6. Echo \ App \ Lib1 \ MyFunction (). "\ n ";
  7. Echo \ App \ Lib1 \ MyClass: WhoAmI (). "\ n ";
  8. ?>

In myapp. php does not define a namespace. Therefore, this code exists in the global space. any direct reference to MYCONST, MyFunction, and MyClass will fail because they exist in the App \ Lib1 namespace, to call the code in lib1.php, we can add a prefix before the \ App \ Lib1 namespace to define a fully qualified name. Below is how I load the myapp. php output result:

 
  1. App\Lib1\MYCONST    
  2. App \ Lib1 \ MyFunction
  3. App \ Lib1 \ MyClass: WhoAmI

Full qualified names can become long, defining long names such as App-Lib1-MyClass, has some obvious benefits.

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.