How to Use the PHP namespace (1): Basic knowledge

Source: Internet
Author: User
Tags zend framework

Namespace is an important concept. This series of articles details how PHP supports and uses namespaces. Address: http://www.sitepoint.com/php-53-namespaces-basics/


Namespace is one of many important updates in PHP 5.3. It will make C # and Java developers feel friendly and hope to make the PHP application program structure better.

Why do I need a namespace?As your PHP code base grows, the risk of accidentally redefining previously declared functions is also increasing. This problem deteriorates when a third-party component or plug-in is introduced. What happens if multi-part code implements the "Database" or "user" class? Until now, the only solution is to name a long class or function name. For example, WordPress uses the "WP _" prefix before each name. Zend framework uses highly descriptive naming conventions, which leads to lengthy class names such as zend_search_inclue_analysis_analyzer_common_text_caseinsensitive. Namespace can solve the naming conflict problem. PHP constants, classes, and functions can be organized into code libraries using namespaces.
How to define a namespace?By default, all constants, classes, and functions are named in the global space, just as before PHP does not support namespaces. Namespace keywords are used at the top of the PHP file. This keyword cannot contain PHP or HTML code, but can only contain blank characters or the declare keyword.
<?php// define this code in the 'MyProject' namespacenamespace MyProject;// ... code ...

The code after the namespace keyword is included in the "myproject" namespace. The namespace cannot be nested or declared multiple times in the same code (only the last one will be recognized ). However, you can define multiple namespace codes in the same file.

<?phpnamespace MyProject1;// PHP code for the MyProject1 namespacenamespace MyProject2;// PHP code for the MyProject2 namespace// Alternative syntaxnamespace MyProject3 {// PHP code for the MyProject3 namespace}?>

Although this syntax is feasible, it is wise to define a namespace for each file.

Sub-namespacePHP allows you to define the namespace level. The subnamespace is separated by a backslash. For example:
  • Myproject \ subname
  • Myproject \ database \ mysql
  • CompanyName \ myproject \ library \ common \ widget1
Call the code with a namespaceDefine a constant, a function, and a class in the app \ lib1 namespace in the lib1.php file. Lib1.php
<?php// application library 1namespace App\Lib1;const MYCONST = 'App\Lib1\MYCONST';function MyFunction() {     return __FUNCTION__;}class MyClass {     static function WhoAmI() {          return __METHOD__;     }}?>

Include this file in another PHP file.

MyApp. php

<?phpheader('Content-type: text/plain');require_once('lib1.php');echo \App\Lib1\MYCONST . "\n";echo \App\Lib1\MyFunction() . "\n";echo \App\Lib1\MyClass::WhoAmI() . "\n";?>

Because no namespace is defined in MyApp. php, all code is in the global space. Any direct reference to myconst, myfunction, or myclass will fail because they are in the app \ lib1 namespace. To call the code in lib1.php, you must add the "\ app \ lib1" prefix to form a fully qualified name (fully-qualified names ). The running result of MyApp. php is:
App\Lib1\MYCONSTApp\Lib1\MyFunctionApp\Lib1\MyClass::WhoAmI

A fully qualified name can also be long, and it is not significantly advantageous for long class names such as App-Lib1-MyClass. Therefore, in the next article, we will discuss namespaces and take a closer look at how PHP handles namespaces.



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.