PHP kernel-namespaces tips for sharing

Source: Internet
Author: User

In Wikipedia, the definition of a namespace is: the namespace (English: Namespace) represents the context of the identifier (identifier).

An identifier can be defined in more than one namespace, and its meaning in different namespaces is mutually irrelevant. In a programming language, a namespace is a special scope that contains an identifier that is in that scope, and itself is represented by an identifier, which organizes a series of logically related identifiers together with an identifier. The scopes of functions and classes can be seen as implicit namespaces, which are inextricably linked to visibility, accessibility, and object life cycles.


1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant within PHP.

2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) and improve the readability of the source code.


PHP supports namespace features starting with version 5.3.0. Look at an example that defines and uses a namespace:


<?phpnamespace tipi;class Exception {public    static $var = ' Think in PHP internal ';} const E_all = "E_all in Tipi"; function strlen () {    echo ' strlen in Tipi ';} echo Exception:: $var; Echo strlen (Exception:: $var);

1. Example:



2. Definition of namespaces


3. Use of namespaces

As an example of a function call, the Zend_do_begin_function_call function is called when a function needs to be called. In this function, when a namespace is used, the function name is checked, and the function called is zend_resolve_non_class_name. In the Zend_resolve_non_class_name function, it is judged by type and returns the relevant result:

1. fully qualified name function : The program first makes this judgment, and its judgment is based on whether the first character is "\", in which case it is returned directly at parse time. A global call, such as \strlen, that begins with \ or a \tipi\exception call similar to the previously defined.

2. All unqualified and qualified names (not fully qualified names) : Determines whether an alias is aliased based on the current import rule program and takes the corresponding namespace name from the Hashtable that stores the alias during compilation. Merge it with the existing function name. The storage and generation of aliases are described in the following sections,

3. inside the namespace : all qualified names that are not translated according to the import rule are preceded by the current namespace name. Finally, if the current namespace is determined, the final program returns a function name that incorporates the namespace.

4. Aliases/import

Allowing the external fully qualified name to be referenced or imported through an alias is an important feature of the namespace.

This is somewhat similar to the ability to create symbolic connections to other files or directories in a UNIX-like file system. The PHP namespace supports two ways to use aliases or imports: Use aliases for class names, or use aliases for namespace names.

Related Article

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.