Namespaces in PHP 5.3: Have you used them?

Source: Internet
Author: User
Tags define functions naming convention php file php code return zend framework

Namespaces are the most important change in PHP 5.3, and the word is so familiar to C # and Java developers that they can finally better change the structure of the PHP application.

Why do we need namespaces?

With the growth of your PHP code base, the risk is also higher when you modify the previously defined functions and class names, and when you try to add a third party component or plug-in, what happens if you have two or more code sets that implement a "Database" and a "User" class?

Until now, the only solution is to use long class/function names, such as the prefix "Wp_" used by WordPress for each class and functor name, and the Zend framework uses a very descriptive naming convention that causes the class name to be very verbose, such as:

Zend_search_lucene_analysis_analyzer_common_text_caseinsensitive

A naming conflict problem can be resolved using namespaces, and PHP constants, classes, and functions can be grouped into a namespace library.

How do I define namespaces?

By default, all constants, classes, and function names are placed in global space, just as they were before the PHP support namespace.

Use a keyword namespace at the top of the PHP file to define the namespace, which must be the first command (except declare), before it can appear without PHP code, HTML, or spaces. Such as:

    1. 		< PHP
    2. Define this code in the ' MyProject ' namespace
    3. namespace MyProject;
    4. ... code ...

The following code in this line is assigned to the MyProject namespace, it is not possible to nest namespaces or define multiple namespaces for the same code block, if you do, only the last namespace can be identified, but you can define different namespace codes in the same file, such as:

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

Although it is OK to do so, but I suggest you do not do so, it is best to define only one namespace in each file, lest you get confused.

Child namespaces

PHP allows you to define a hierarchy of namespaces so that the libraries can be subdivided, and the child namespaces are separated by a backslash character (\), such as:

Myproject\subname

Myproject\database\mysql

Companyname\myproject\library\common\widget1

Calling namespace code

In the lib1.php file we define a constant, a function, and a class using the App\lib1 namespace namespace, such as:

lib1.php

    1. 		< PHP
    2. Application Library 1
    3. namespace APP\LIB1;
    4. Const MYCONST = ' app\lib1\myconst ';
    5. function MyFunction () {
    6. return __function__;
    7. }
    8. Class MyClass {
    9. static function WhoAmI () {
    10. return __method__;
    11. }
    12. }
    13. ?>

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

myapp.php

    1. 		< PHP
    2. Header (' Content-type:text/plain ');
    3. Require_once (' lib1.php ');
    4. Echo \app\lib1\myconst. "\ n";
    5. Echo \app\lib1\myfunction (). "\ n";
    6. Echo \app\lib1\myclass::whoami (). "\ n";
    7. ?>

There is no namespace defined in myapp.php, so this code has global space, and any direct references to Myconst, MyFunction, and MyClass fail because they exist in the App\lib1 namespace. To invoke the code in lib1.php, we can add a prefix to the \APP\LIB1 namespace to define a fully qualified name, the following is the output of my load myapp.php:

    1. 		App\lib1\myconst
    2. App\lib1\myfunction
    3. App\lib1\myclass::whoami

Fully qualified names can become very long, defining long names, such as App-lib1-myclass, with 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.